0
$\begingroup$

I have imported data as a Pandas DataFrame which has string representing months of various years such as: 'Nov-23', 'Mar-24' etc.
I would like to parse these strings into proper Pandas Periods. If I run (in September 2023):

>>> pd.Period.now('M')

Period('2023-09', 'M')

I get a Period object that seems to represent September in 2023 as expected. However, if I try:

>>> pd.Period('Sep-23', freq='M')

Period('1-09', 'M')

I do get a Period object seemingly representing September, but it seems to discard the information that it is in 2023.

How can I correctly parse time period strings like 'Sep-23' into Pandas Periods that retain their year?

$\endgroup$

1 Answer 1

1
$\begingroup$

Because it's not parsing it correctly

> pd.Period(pd.to_datetime('Sep-23',format="%b-%y"),freq='M')
Period('2023-09', 'M')
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.