Computer Science, asked by saiswastiparija3126, 1 year ago

How to convert pandas offset to Python date?

Answers

Answered by honeygupta4
0

Hey mate!!

here's your answer....

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. For 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))

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. For 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()))

This will give the output:

2017-12-30

2017-12-30

<class 'datetime.date'>

Hope it will be helpful for you

*please mark as brainlist *

Similar questions