Computer >> Computer tutorials >  >> Programming >> Python

How to convert pandas offset to Python date?


When you subtract the pandas from a date object, you get a pandas Timestamp object. You can convert this object to a String format date or Date object(Standard Python date). Or you can use the timedelta object from the datetime library. 

Example

from pandas.tseries.frequencies import to_offset
import pandas as pd
dt = pd.to_datetime('2018-01-04') - to_offset("5D")
print(type(dt))

Output

This will give the output −

<class 'pandas._libs.tslib.Timestamp'>

To convert this to a string of a given format, you can use the strftime function. To convert it to Date object, you can use the date() function on this object. 

Example

from pandas.tseries.frequencies import to_offset
import pandas as pd
dt = pd.to_datetime('2018-01-04') - to_offset("5D")
print(dt.strftime('%Y-%m-%d'))
print(dt.date())
print(type(dt.date()))

Output

This will give the output −

2017-12-30
2017-12-30
<class 'datetime.date'>