Skip to content

Commit b0bd2d8

Browse files
committed
refactor name method of PeriodDtype, refactor __arrow_array__ and add test for ValueError in test_period.py
1 parent a404fa2 commit b0bd2d8

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

pandas/_libs/tslibs/period.pyx

-1
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,6 @@ def extract_ordinals(ndarray values, freq) -> np.ndarray:
15441544
# if we don't raise here, we'll segfault later!
15451545
raise TypeError("extract_ordinals values must be object-dtype")
15461546

1547-
freqstr = Period._maybe_convert_freq(freq).freqstr
15481547
freqstr = freq_to_period_freqstr(freq.n, freq.name)
15491548

15501549
for i in range(n):

pandas/core/arrays/period.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from pandas._libs.tslibs.dtypes import (
3838
OFFSET_TO_PERIOD_FREQSTR,
3939
FreqGroup,
40+
freq_to_period_freqstr,
4041
)
4142
from pandas._libs.tslibs.fields import isleapyear_arr
4243
from pandas._libs.tslibs.offsets import (
@@ -412,10 +413,7 @@ def __arrow_array__(self, type=None):
412413
f"Not supported to convert PeriodArray to '{type}' type"
413414
)
414415

415-
if self.freqstr == "ME":
416-
period_type = ArrowPeriodType("M")
417-
else:
418-
period_type = ArrowPeriodType(self.freqstr)
416+
period_type = ArrowPeriodType(freq_to_period_freqstr(1, self.freqstr))
419417
storage_array = pyarrow.array(self._ndarray, mask=self.isna(), type="int64")
420418
return pyarrow.ExtensionArray.from_storage(period_type, storage_array)
421419

pandas/core/dtypes/dtypes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from pandas._libs.tslibs.dtypes import (
4343
PeriodDtypeBase,
4444
abbrev_to_npy_unit,
45+
freq_to_period_freqstr,
4546
)
4647
from pandas.compat import pa_version_under7p0
4748
from pandas.errors import PerformanceWarning
@@ -999,8 +1000,7 @@ def __str__(self) -> str_type:
9991000

10001001
@property
10011002
def name(self) -> str_type:
1002-
dname = {"ME": "period[M]"}
1003-
return dname.get(self._freqstr, f"period[{self._freqstr}]")
1003+
return "".join(["period[", freq_to_period_freqstr(1, self._freqstr), "]"])
10041004

10051005
@property
10061006
def na_value(self) -> NaTType:

pandas/tests/scalar/period/test_period.py

+6
Original file line numberDiff line numberDiff line change
@@ -1590,3 +1590,9 @@ def test_invalid_frequency_error_message():
15901590
msg = "Invalid frequency: <WeekOfMonth: week=0, weekday=0>"
15911591
with pytest.raises(ValueError, match=msg):
15921592
Period("2012-01-02", freq="WOM-1MON")
1593+
1594+
1595+
def test_invalid_frequency_period_error_message():
1596+
msg = "Invalid frequency: ME"
1597+
with pytest.raises(ValueError, match=msg):
1598+
Period("2012-01-02", freq="ME")

0 commit comments

Comments
 (0)