Skip to content

Commit 579bf89

Browse files
committed
FIX: make sure Dataframes with a DatetimeIndex come out as str labels as before (fixes larray-project#1187)
In fact, this is about reintroducing a bug I fixed by accident to avoid breaking users existing code in a bugfix release
1 parent 7d5ea69 commit 579bf89

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

larray/inout/pandas.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ def simple_index_to_labels(idx: pd.Index, sort=True, keep_object=True) -> np.nda
5959
labels = idx.to_numpy()
6060
else:
6161
labels = np.asarray(idx.to_list())
62+
# this is a bug introduced on purpose to keep backward compatibility
63+
# (see issue #1187)
64+
if isinstance(idx, pd.DatetimeIndex):
65+
labels = labels.astype(str)
6266
return labels
6367

6468

larray/tests/test_array.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4134,6 +4134,8 @@ def test_read_excel_pandas():
41344134

41354135

41364136
def test_from_lists():
4137+
from datetime import datetime
4138+
41374139
expected = ndtest((2, 2, 3))
41384140

41394141
# simple
@@ -4200,6 +4202,15 @@ def test_from_lists():
42004202
['c1', 'FO', 0, 0, 2]], sort_columns=True)
42014203
assert_larray_equal(sorted_arr, expected)
42024204

4205+
# with datetime labels
4206+
res = from_lists([['str', 'date', 'value'],
4207+
['abc', datetime.now(), 1]],
4208+
nb_axes=3)
4209+
# this is what we SHOULD return but we do not so far to avoid breaking
4210+
# backward compatibility (see issue #1187)
4211+
# assert res.axes[1].dtype == 'datetime64[ns]'
4212+
assert res.axes[1].dtype == '<U48'
4213+
42034214

42044215
def test_to_series():
42054216
# simple test

0 commit comments

Comments
 (0)