Open corralien opened 1 year ago
The issue is that it is trying to do a factorize
with sort=True
:
>>> p1 = pd.Period('2022Q1', 'Q-DEC')
>>> p2 = pd.Period('2019', 'A-DEC')
>>> arr = np.array([p1, p2], dtype=object)
>>> pd.factorize(arr, sort=True)
IncompatibleFrequency: Input has different freq=Q-DEC from Period(freq=A-DEC)
>>> pd.factorize(arr, sort=False)
(array([0, 1]),
array([Period('2022Q1', 'Q-DEC'), Period('2019', 'A-DEC')], dtype=object))
My guess is that the call to factorize
shouldn't sort here, but investigations welcome.
Pandas version checks
[X] I have checked that this issue has not already been reported.
[X] I have confirmed this bug exists on the latest version of pandas.
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
When using
keys
parameter onpd.concat
, this code raisesIncompatibleFrequency
exception but it's not the case if you don't specifykeys
because both period indexes are converted to a simpleIndex
ofPeriod
instances.Expected Behavior
I don't except anything just to understand the behavior. I answered to this SO question. In my original answer, I use
pd.concat
withkeys
to create aMultiIndex
. As it doesn't work as expected, I downcast thePeriod
instances to string.Maybe instead of raising an exception, convert as
Index
(like if I don't usekeys
parameter) and raises aUserWarning
orRuntimeWarning
about the incompatibility ofPeriodIndex
?(You are doing a great job with Pandas, thank you so much!)
Installed Versions