Skip to content

to_datetime loses freq on DatetimeIndex with offset #6562

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
cancan101 opened this issue Mar 6, 2014 · 8 comments
Closed

to_datetime loses freq on DatetimeIndex with offset #6562

cancan101 opened this issue Mar 6, 2014 · 8 comments
Labels
Bug Datetime Datetime data dtype Frequency DateOffsets

Comments

@cancan101
Copy link
Contributor

It would be cool if the offset were kept. Perhaps even an error should be raised if not all of the Timestamps have the same offset:

In [36]:
dti = pd.to_datetime([pd.Timestamp("2014-1-1", offset="M"),])
dti

Out[36]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2014-01-01]
Length: 1, Freq: None, Timezone: None

See #6560

@jreback jreback added this to the 0.15.0 milestone Mar 22, 2014
@jreback jreback modified the milestones: 0.16.0, Next Major Release Mar 3, 2015
@mroeschke
Copy link
Member

Timestamp no longer accepts the offset keyword (deprecated and removed). Closing.

@jbrockmendel
Copy link
Member

@mroeschke I think this is still an issue with the freq keyword:

>>> pd.Timestamp('now', freq='D')
Timestamp('2018-07-07 08:17:00.395067', freq='D')

>>> pd.to_datetime(pd.Timestamp('now', freq='D'))
Timestamp('2018-07-07 08:17:08.939068', freq='D')

>>> pd.to_datetime([pd.Timestamp('now', freq='D')])
DatetimeIndex(['2018-07-07 08:17:14.227013'], dtype='datetime64[ns]', freq=None)

@mroeschke
Copy link
Member

Oh I see. I had also rationalized closing this in light of #15146, but I see you mentioned that removing freq from Timestamp is non-trivial. Do you believe that's still the case?

@jbrockmendel
Copy link
Member

Do you believe that's still the case?

Backwards-compat would be annoying, not sure if anyone would really complain though.

Getting rid of it would break a behavior that is nice for testing that (dti + other == [x + other for x in dti]).all(). Probably not a good enough reason to keep the Timestamp.freq attribute if it isn't otherwise needed.

But as long as the attribute does exist, I think to_datetime should preserve it.

@mroeschke
Copy link
Member

Sounds good. Reopening.

@jbrockmendel
Copy link
Member

@mroeschke didn't this get solved recently?

@mroeschke
Copy link
Member

This now works for the scalar case but still not the array case. But should the DatetimeIndex have a freq in this case with just 1 element?

In [5]: pd.__version__
Out[5]: '0.24.0.dev0+1010.ge413c491e'

In [6]: pd.to_datetime(pd.Timestamp("2014-1-1", freq="M"))
Out[6]: Timestamp('2014-01-01 00:00:00', freq='M')

In [7]: pd.to_datetime([pd.Timestamp("2014-1-1", freq="M")])
Out[7]: DatetimeIndex(['2014-01-01'], dtype='datetime64[ns]', freq=None)

@mroeschke mroeschke changed the title to_datetime loses freq on Timestamp with offset to_datetime loses freq on DatetimeIndex with offset Mar 31, 2020
@mroeschke mroeschke added Bug and removed API Design labels Mar 31, 2020
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
@lukemanley
Copy link
Member

This was once closed and then reopened due to Timestamp.freq but can now be closed again since freq has been deprecated and removed. 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
Projects
None yet
Development

No branches or pull requests

5 participants