Skip to content

DatetimeIndex.map() preserves freq only for some functions #22312

@toobaz

Description

@toobaz

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.

  1. 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.
  2. 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

No one assigned

    Labels

    BugDatetimeDatetime data dtypeFrequencyDateOffsetsIndexRelated to the Index class or subclasses

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions