How to convert pandas offset to Python date?

PythonServer Side ProgrammingProgramming

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'>
raja
Published on 05-Jan-2018 18:11:39
Advertisements