Skip to content

BUG: PeriodIndex constructor doesn't work with Series objects #7712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/v0.14.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,4 @@ Bug Fixes

- Bug in ``to_sql`` taking the boolean column as text column (:issue:`7678`)
- Bug in grouped `hist` doesn't handle `rot` kw and `sharex` kw properly (:issue:`7234`)

- Bug (regression) in ``PeriodIndex`` constructor when passed ``Series`` objects (:issue:`7701`).
6 changes: 3 additions & 3 deletions pandas/tseries/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import pandas.core.common as com
from pandas.core.common import (isnull, _INT64_DTYPE, _maybe_box,
_values_from_object)
_values_from_object, ABCSeries)
from pandas import compat
from pandas.lib import Timestamp
import pandas.lib as lib
Expand Down Expand Up @@ -1261,13 +1261,13 @@ def _range_from_fields(year=None, month=None, quarter=None, day=None,
def _make_field_arrays(*fields):
length = None
for x in fields:
if isinstance(x, (list, np.ndarray)):
if isinstance(x, (list, np.ndarray, ABCSeries)):
if length is not None and len(x) != length:
raise ValueError('Mismatched Period array lengths')
elif length is None:
length = len(x)

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

return arrays
Expand Down
9 changes: 9 additions & 0 deletions pandas/tseries/tests/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,15 @@ def test_constructor_nat(self):
self.assertRaises(
ValueError, period_range, start='2011-01-01', end='NaT', freq='M')

def test_constructor_year_and_quarter(self):
year = pd.Series([2001, 2002, 2003])
Copy link
Contributor

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 here?

quarter = year - 2000
idx = PeriodIndex(year=year, quarter=quarter)
strs = ['%dQ%d' % t for t in zip(quarter, year)]
lops = list(map(Period, strs))
p = PeriodIndex(lops)
tm.assert_index_equal(p, idx)

def test_is_(self):
create_index = lambda: PeriodIndex(freq='A', start='1/1/2001',
end='12/1/2009')
Expand Down