Skip to content

Commit 716689a

Browse files
authored
REGR: Fix construction of PeriodIndex from strings (#33304)
1 parent 4a74463 commit 716689a

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ Datetimelike
401401
- :class:`Timestamp` raising confusing error message when year, month or day is missing (:issue:`31200`)
402402
- Bug in :class:`DatetimeIndex` constructor incorrectly accepting ``bool``-dtyped inputs (:issue:`32668`)
403403
- Bug in :meth:`DatetimeIndex.searchsorted` not accepting a ``list`` or :class:`Series` as its argument (:issue:`32762`)
404+
- Bug where :meth:`PeriodIndex` raised when passed a :class:`Series` of strings (:issue:`26109`)
404405
- Bug in :class:`Timestamp` arithmetic when adding or subtracting a ``np.ndarray`` with ``timedelta64`` dtype (:issue:`33296`)
405406

406407
Timedelta

pandas/core/arrays/period.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -831,11 +831,11 @@ def period_array(
831831
"""
832832
if is_datetime64_dtype(data):
833833
return PeriodArray._from_datetime64(data, freq)
834-
if isinstance(data, (ABCPeriodIndex, ABCSeries, PeriodArray)):
834+
if is_period_dtype(data):
835835
return PeriodArray(data, freq)
836836

837837
# other iterable of some kind
838-
if not isinstance(data, (np.ndarray, list, tuple)):
838+
if not isinstance(data, (np.ndarray, list, tuple, ABCSeries)):
839839
data = list(data)
840840

841841
data = np.asarray(data)

pandas/tests/arrays/test_datetimelike.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import pandas._testing as tm
1111
from pandas.core.arrays import DatetimeArray, PeriodArray, TimedeltaArray
1212
from pandas.core.indexes.datetimes import DatetimeIndex
13-
from pandas.core.indexes.period import PeriodIndex
13+
from pandas.core.indexes.period import Period, PeriodIndex
1414
from pandas.core.indexes.timedeltas import TimedeltaIndex
1515

1616

@@ -902,3 +902,13 @@ def test_searchsorted_datetimelike_with_listlike_invalid_dtype(values, arg):
902902
msg = "[Unexpected type|Cannot compare]"
903903
with pytest.raises(TypeError, match=msg):
904904
values.searchsorted(arg)
905+
906+
907+
@pytest.mark.parametrize("klass", [list, tuple, np.array, pd.Series])
908+
def test_period_index_construction_from_strings(klass):
909+
# https://github.com/pandas-dev/pandas/issues/26109
910+
strings = ["2020Q1", "2020Q2"] * 2
911+
data = klass(strings)
912+
result = PeriodIndex(data, freq="Q")
913+
expected = PeriodIndex([Period(s) for s in strings])
914+
tm.assert_index_equal(result, expected)

pandas/tests/arrays/test_period.py

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def test_registered():
3737
([pd.Period("2017", "D"), None], None, [17167, iNaT]),
3838
(pd.Series(pd.date_range("2017", periods=3)), None, [17167, 17168, 17169]),
3939
(pd.date_range("2017", periods=3), None, [17167, 17168, 17169]),
40+
(pd.period_range("2017", periods=4, freq="Q"), None, [188, 189, 190, 191]),
4041
],
4142
)
4243
def test_period_array_ok(data, freq, expected):

0 commit comments

Comments
 (0)