You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Because of the restricted time range covered by Timestamp objects, I am building my code only using Period objects and indexes, as recommended by the doc. However, when using timestamps I could conveniently pass the object to a f-string with proper formatting strings. This doesn't work with Period objects.
But I see that Period is not implemented in pure python, so I'm not sure how this impacts the implementation.
Alternative Solutions
The alternative is to cast the Periods to string before formatting the f-string, but that decouples the date formatting from the template itself.
Additional Context
Not super relevant to the issue, but in my context, the f-string are user-provided. My goal is to give that power to the user : one can choose how the dates are printed. The specific specific context where this bug was raised in my current code is when I generate a filename from a set of attributes, including some dates. I want the user to be able to choose between printing only the year or the year and month or anything else.
The text was updated successfully, but these errors were encountered:
@aulemahal You can monkeypatch your idea in the meantime
import pandas as pd
def patch_Period_format():
def __format__(self, fmt):
# same implementation as datetime.datetime.__format__
if not isinstance(fmt, str):
raise TypeError("must be str, not %s" % type(fmt).__name__)
if len(fmt) != 0:
return self.strftime(fmt)
return str(self)
pd.Period.__format__ = __format__
patch_Period_format()
p = pd.Period('2262-04-11', 'H')
f"{p:%Y-%m}"
> '2262-04'
Feature Type
Adding new functionality to pandas
Changing existing functionality in pandas
Removing existing functionality in pandas
Problem Description
Because of the restricted time range covered by
Timestamp
objects, I am building my code only usingPeriod
objects and indexes, as recommended by the doc. However, when using timestamps I could conveniently pass the object to a f-string with proper formatting strings. This doesn't work withPeriod
objects.Example:
Feature Description
I think the easiest way to implement this would be:
But I see that
Period
is not implemented in pure python, so I'm not sure how this impacts the implementation.Alternative Solutions
The alternative is to cast the Periods to string before formatting the f-string, but that decouples the date formatting from the template itself.
Additional Context
Not super relevant to the issue, but in my context, the f-string are user-provided. My goal is to give that power to the user : one can choose how the dates are printed. The specific specific context where this bug was raised in my current code is when I generate a filename from a set of attributes, including some dates. I want the user to be able to choose between printing only the year or the year and month or anything else.
The text was updated successfully, but these errors were encountered: