Skip to content

Commit 7707a20

Browse files
committed
Merge pull request #7712 from cpcloud/period-index-constructor
BUG: PeriodIndex constructor doesn't work with Series objects
2 parents 9d71aae + eb9d1bc commit 7707a20

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

doc/source/v0.14.1.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,4 @@ Bug Fixes
291291

292292
- Bug in ``to_sql`` taking the boolean column as text column (:issue:`7678`)
293293
- Bug in grouped `hist` doesn't handle `rot` kw and `sharex` kw properly (:issue:`7234`)
294-
294+
- Bug (regression) in ``PeriodIndex`` constructor when passed ``Series`` objects (:issue:`7701`).

pandas/tseries/period.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import pandas.core.common as com
1616
from pandas.core.common import (isnull, _INT64_DTYPE, _maybe_box,
17-
_values_from_object)
17+
_values_from_object, ABCSeries)
1818
from pandas import compat
1919
from pandas.lib import Timestamp
2020
import pandas.lib as lib
@@ -1261,13 +1261,13 @@ def _range_from_fields(year=None, month=None, quarter=None, day=None,
12611261
def _make_field_arrays(*fields):
12621262
length = None
12631263
for x in fields:
1264-
if isinstance(x, (list, np.ndarray)):
1264+
if isinstance(x, (list, np.ndarray, ABCSeries)):
12651265
if length is not None and len(x) != length:
12661266
raise ValueError('Mismatched Period array lengths')
12671267
elif length is None:
12681268
length = len(x)
12691269

1270-
arrays = [np.asarray(x) if isinstance(x, (np.ndarray, list))
1270+
arrays = [np.asarray(x) if isinstance(x, (np.ndarray, list, ABCSeries))
12711271
else np.repeat(x, length) for x in fields]
12721272

12731273
return arrays

pandas/tseries/tests/test_period.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,15 @@ def test_constructor_nat(self):
12811281
self.assertRaises(
12821282
ValueError, period_range, start='2011-01-01', end='NaT', freq='M')
12831283

1284+
def test_constructor_year_and_quarter(self):
1285+
year = pd.Series([2001, 2002, 2003])
1286+
quarter = year - 2000
1287+
idx = PeriodIndex(year=year, quarter=quarter)
1288+
strs = ['%dQ%d' % t for t in zip(quarter, year)]
1289+
lops = list(map(Period, strs))
1290+
p = PeriodIndex(lops)
1291+
tm.assert_index_equal(p, idx)
1292+
12841293
def test_is_(self):
12851294
create_index = lambda: PeriodIndex(freq='A', start='1/1/2001',
12861295
end='12/1/2009')

0 commit comments

Comments
 (0)