Skip to content

Commit 7a9cfaf

Browse files
bolliger32jcrist
authored andcommitted
keep index name with to_datetime (dask#4905)
* keep index name with to_datetime * allow users to pass meta * Update dask/dataframe/core.py put meta as explicit kwarg Co-Authored-By: Matthew Rocklin <[email protected]> * Update dask/dataframe/core.py remove meta kwargs.pop Co-Authored-By: Matthew Rocklin <[email protected]> * remove test for index * allow index
1 parent abc86d3 commit 7a9cfaf

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

dask/dataframe/core.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4877,8 +4877,15 @@ def maybe_shift_divisions(df, periods, freq):
48774877

48784878

48794879
@wraps(pd.to_datetime)
4880-
def to_datetime(arg, **kwargs):
4881-
meta = pd.Series([pd.Timestamp('2000')])
4880+
def to_datetime(arg, meta=None, **kwargs):
4881+
if meta is None:
4882+
if isinstance(arg, Index):
4883+
meta = pd.DatetimeIndex([])
4884+
meta.name = arg.name
4885+
else:
4886+
meta = pd.Series([pd.Timestamp('2000')])
4887+
meta.index = meta.index.astype(arg.index.dtype)
4888+
meta.index.name = arg.index.name
48824889
return map_partitions(pd.to_datetime, arg, meta=meta, **kwargs)
48834890

48844891

dask/dataframe/tests/test_dataframe.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3261,15 +3261,20 @@ def test_to_datetime():
32613261
df = pd.DataFrame({'year': [2015, 2016],
32623262
'month': [2, 3],
32633263
'day': [4, 5]})
3264+
df.index.name = 'ix'
32643265
ddf = dd.from_pandas(df, npartitions=2)
32653266

32663267
assert_eq(pd.to_datetime(df), dd.to_datetime(ddf))
32673268

32683269
s = pd.Series(['3/11/2000', '3/12/2000', '3/13/2000'] * 100)
3269-
ds = dd.from_pandas(s, npartitions=10)
3270+
s.index = s.values
3271+
ds = dd.from_pandas(s, npartitions=10, sort=False)
32703272

32713273
assert_eq(pd.to_datetime(s, infer_datetime_format=True),
32723274
dd.to_datetime(ds, infer_datetime_format=True))
3275+
assert_eq(pd.to_datetime(s.index, infer_datetime_format=True),
3276+
dd.to_datetime(ds.index, infer_datetime_format=True),
3277+
check_divisions=False)
32733278

32743279

32753280
def test_to_timedelta():

0 commit comments

Comments
 (0)