-
-
Notifications
You must be signed in to change notification settings - Fork 18.9k
Closed
Labels
BugDatetimeDatetime data dtypeDatetime data dtypeFrequencyDateOffsetsDateOffsetsIndexRelated to the Index class or subclassesRelated to the Index class or subclasses
Description
Code Sample, a copy-pastable example if possible
In [2]: idx = pd.date_range(start='2010-08-08', end='2010-08-15')
In [3]: idx.map(lambda x : x + pd.Timedelta('3 days'))
Out[3]:
DatetimeIndex(['2010-08-11', '2010-08-12', '2010-08-13', '2010-08-14',
'2010-08-15', '2010-08-16', '2010-08-17', '2010-08-18'],
dtype='datetime64[ns]', freq='D')
In [4]: idx.map(lambda x : pd.Timestamp(x) + pd.Timedelta('3 days'))
Out[4]:
DatetimeIndex(['2010-08-11', '2010-08-12', '2010-08-13', '2010-08-14',
'2010-08-15', '2010-08-16', '2010-08-17', '2010-08-18'],
dtype='datetime64[ns]', freq=None)
Problem description
This results from the combination of two problems.
DatetimeIndex.map()
does some pretty horrible thing: it first tries to call the passed function on the calling instance, and only if this fails (according to some debatable definition of "failure") it calls it on each element of the instance.freq
is preserved if the first call goes through; otherwise it is not.- pd.Timestamp(timestamp) looses freq #22311
... but problem 1. must fixed anyway - the behavior doesn't make much sense, is DatetimeIndex
-specific, and undocumented.
Expected Output
Out[3]
and Out[4]
should coincide.
Output of pd.show_versions()
[paste the output of pd.show_versions()
here below this line]
Metadata
Metadata
Assignees
Labels
BugDatetimeDatetime data dtypeDatetime data dtypeFrequencyDateOffsetsDateOffsetsIndexRelated to the Index class or subclassesRelated to the Index class or subclasses