pandas.Timestamp.replace#

Timestamp.replace(year=None, month=None, day=None, hour=None, minute=None, second=None, microsecond=None, nanosecond=None, tzinfo=<class 'object'>, fold=None)#

Implements datetime.replace, handles nanoseconds.

This method creates a new Timestamp object by replacing the specified fields with new values. The new Timestamp retains the original fields that are not explicitly replaced. This method handles nanoseconds, and the tzinfo parameter allows for timezone replacement without conversion.

Parameters:
yearint, optional

The year to replace. If None, the year is not changed.

monthint, optional

The month to replace. If None, the month is not changed.

dayint, optional

The day to replace. If None, the day is not changed.

hourint, optional

The hour to replace. If None, the hour is not changed.

minuteint, optional

The minute to replace. If None, the minute is not changed.

secondint, optional

The second to replace. If None, the second is not changed.

microsecondint, optional

The microsecond to replace. If None, the microsecond is not changed.

nanosecondint, optional

The nanosecond to replace. If None, the nanosecond is not changed.

tzinfotz-convertible, optional

The timezone information to replace. If None, the timezone is not changed.

foldint, optional

The fold information to replace. If None, the fold is not changed.

Returns:
Timestamp

A new Timestamp object with the specified fields replaced.

See also

Timestamp

Represents a single timestamp, similar to datetime.

to_datetime

Converts various types of data to datetime.

Notes

The replace method does not perform timezone conversions. If you need to convert the timezone, use the tz_convert method instead.

Examples

Create a timestamp object:

>>> ts = pd.Timestamp('2020-03-14T15:32:52.192548651', tz='UTC')
>>> ts
Timestamp('2020-03-14 15:32:52.192548651+0000', tz='UTC')

Replace year and the hour:

>>> ts.replace(year=1999, hour=10)
Timestamp('1999-03-14 10:32:52.192548651+0000', tz='UTC')

Replace timezone (not a conversion):

>>> import zoneinfo
>>> ts.replace(tzinfo=zoneinfo.ZoneInfo('US/Pacific'))
Timestamp('2020-03-14 15:32:52.192548651-0700', tz='US/Pacific')

Analogous for pd.NaT:

>>> pd.NaT.replace(tzinfo=zoneinfo.ZoneInfo('US/Pacific'))
NaT