Skip to content
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ Datetimelike
- :class:`Timestamp` raising confusing error message when year, month or day is missing (:issue:`31200`)
- Bug in :class:`DatetimeIndex` constructor incorrectly accepting ``bool``-dtyped inputs (:issue:`32668`)
- Bug in :meth:`DatetimeIndex.searchsorted` not accepting a ``list`` or :class:`Series` as its argument (:issue:`32762`)
- Bug where :meth:`PeriodIndex` raised when passed a :class:`Series` of strings (:issue:`26109`)

Timedelta
^^^^^^^^^
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
ensure_object,
is_datetime64_dtype,
is_float_dtype,
is_list_like,
is_period_dtype,
pandas_dtype,
)
Expand Down Expand Up @@ -831,11 +832,11 @@ def period_array(
"""
if is_datetime64_dtype(data):
return PeriodArray._from_datetime64(data, freq)
if isinstance(data, (ABCPeriodIndex, ABCSeries, PeriodArray)):
if is_period_dtype(data):
return PeriodArray(data, freq)

# other iterable of some kind
if not isinstance(data, (np.ndarray, list, tuple)):
if not is_list_like(data):
data = list(data)

data = np.asarray(data)
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,3 +868,10 @@ def test_searchsorted_datetimelike_with_listlike_invalid_dtype(values, arg):
msg = "[Unexpected type|Cannot compare]"
with pytest.raises(TypeError, match=msg):
values.searchsorted(arg)


@pytest.mark.parametrize("klass", [list, tuple, np.array, pd.Series])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could maybe add these cases to test_period_array_ok instead/as well.

def test_period_index_construction_from_strings(klass):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add the issue number.

result = PeriodIndex(klass(["2020Q1", "2020Q2", "2020Q3", "2020Q4"]), freq="Q")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you do data = ...

so that result = statement is the bit being tested. (i.e. we are not testing the klass constructor)

can you also use the data from the OP, since that was not monotonic

expected = pd.period_range(start="2020", periods=4, freq="Q")
tm.assert_index_equal(result, expected)