Skip to content

ENH: replace method for more time-related type #36592

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

Open
GYHHAHA opened this issue Sep 24, 2020 · 7 comments
Open

ENH: replace method for more time-related type #36592

GYHHAHA opened this issue Sep 24, 2020 · 7 comments
Labels
Enhancement Needs Discussion Requires discussion from core team before further action replace replace method

Comments

@GYHHAHA
Copy link
Contributor

GYHHAHA commented Sep 24, 2020

Now we can only implement replace on Timestamp, I think TimedeltaIndex, Timedelta, DatetimeIndex and even Series with datetime64[ns] or timedelta64[ns] all need replace method (works like replace on Timestamp). Is there any plan to do this? (Maybe in pandas 2.0) Thanks!

@GYHHAHA GYHHAHA added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 24, 2020
@jreback
Copy link
Contributor

jreback commented Sep 24, 2020

pls show exactly what you mean

replace is a Timestamp method (inherited from datetime.datetime)

and a Series / index method

@GYHHAHA
Copy link
Contributor Author

GYHHAHA commented Sep 24, 2020

>>>pd.Timestamp('20200101').replace(day=5)
Timestamp('2020-01-05 00:00:00')

Here replace change the day to 5.

>>>ts = pd.date_range('20200101','20200501',freq='MS').to_series()
>>>ts
2020-01-01   2020-01-01
2020-02-01   2020-02-01
2020-03-01   2020-03-01
2020-04-01   2020-04-01
2020-05-01   2020-05-01
Freq: MS, dtype: datetime64[ns]

I want to get this after use replace.

>>>ts.replace(day=5)
2020-01-01   2020-01-05
2020-02-01   2020-02-05
2020-03-01   2020-03-05
2020-04-01   2020-04-05
2020-05-01   2020-05-05
Freq: MS, dtype: datetime64[ns]

Equal to ts.apply(lambda x:x.replace(day=5)).

>>>pd.date_range('20200101','20200501',freq='MS')
DatetimeIndex(['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01',
               '2020-05-01'],
              dtype='datetime64[ns]', freq='MS')
>>>pd.date_range('20200101','20200501',freq='MS').replace(day=5)
DatetimeIndex(['2020-01-05', '2020-02-05', '2020-03-05', '2020-04-05',
               '2020-05-05'],
              dtype='datetime64[ns]', freq='MS')

Also for the index type.

@jreback
Copy link
Contributor

jreback commented Sep 24, 2020

try .dt.replace

@GYHHAHA
Copy link
Contributor Author

GYHHAHA commented Sep 24, 2020

>>>pd.date_range('20200101','20200501',freq='MS').to_series().dt.replace(day=5)
AttributeError: 'DatetimeProperties' object has no attribute 'replace'

this raises error

@jreback
Copy link
Contributor

jreback commented Sep 24, 2020

ok i suppose

though to be honest .replace is not very useful and has a lot of tz implications

much better to simply use offsets

@GYHHAHA
Copy link
Contributor Author

GYHHAHA commented Sep 24, 2020

I see.

But since in here DateOffset arguments that do not end in s is suggested to be deprecated in the future, how to do this (.replace(day=5)) by offset object?

We do this by ts + pd.DateOffset(day=5) formerly.

@dsaxton dsaxton added replace replace method and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 25, 2020
@mroeschke mroeschke added the Needs Discussion Requires discussion from core team before further action label Aug 13, 2021
@hornstrandir
Copy link

Is this still under discussion? I would also find this method very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Needs Discussion Requires discussion from core team before further action replace replace method
Projects
None yet
Development

No branches or pull requests

5 participants