Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
toobaz opened this issue Aug 13, 2018 · 1 comment
Closed

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

toobaz opened this issue Aug 13, 2018 · 1 comment
Labels
Bug Datetime Datetime data dtype Frequency DateOffsets Index Related to the Index class or subclasses

Comments

@toobaz
Copy link
Member

toobaz commented Aug 13, 2018

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]

@toobaz toobaz added Datetime Datetime data dtype Indexing Related to indexing on series/frames, not to indexes themselves Frequency DateOffsets labels Aug 13, 2018
@toobaz toobaz added Index Related to the Index class or subclasses and removed Indexing Related to indexing on series/frames, not to indexes themselves labels Jun 29, 2019
@mroeschke mroeschke added the Bug label Apr 1, 2020
@mroeschke
Copy link
Member

This looks to match now based on mapping refactor that was done for 2.1. I think we have tests for this case now so closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Frequency DateOffsets Index Related to the Index class or subclasses
Projects
None yet
Development

No branches or pull requests

2 participants