Skip to content

Commit

Permalink
BUG: PeriodIndex as DataFrame column should box to Periods #2243 #2281
Browse files Browse the repository at this point in the history
  • Loading branch information
changhiskhan authored and wesm committed Nov 23, 2012
1 parent 5971637 commit eb00827
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
8 changes: 7 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2022,6 +2022,8 @@ def _sanitize_column(self, key, value):

if not isinstance(value, np.ndarray):
value = com._asarray_tuplesafe(value)
elif isinstance(value, PeriodIndex):
value = value.asobject
else:
value = value.copy()
else:
Expand Down Expand Up @@ -2699,7 +2701,11 @@ def _maybe_cast(values):
lev_num = self.columns._get_level_number(col_level)
name_lst[lev_num] = name
name = tuple(name_lst)
new_obj.insert(0, name, _maybe_cast(self.index.values))
if isinstance(self.index, PeriodIndex):
values = self.index.asobject
else:
values = self.index.values
new_obj.insert(0, name, _maybe_cast(values))

new_obj.index = new_index
return new_obj
Expand Down
15 changes: 14 additions & 1 deletion pandas/tseries/tests/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from pandas import Timestamp
from pandas.tseries.frequencies import MONTHS, DAYS
from pandas.tseries.period import Period, PeriodIndex, period_range
from pandas.tseries.index import DatetimeIndex, date_range
from pandas.tseries.index import DatetimeIndex, date_range, Index
from pandas.tseries.tools import to_datetime
import pandas.tseries.period as pmod

Expand Down Expand Up @@ -1300,6 +1300,19 @@ def test_as_frame_columns(self):
ts = df['1/1/2000']
assert_series_equal(ts, df.ix[:, 0])

def test_frame_setitem(self):
rng = period_range('1/1/2000', periods=5)
rng.name = 'index'
df = DataFrame(randn(5, 3), index=rng)

df['Index'] = rng
rs = Index(df['Index'])
self.assert_(rs.equals(rng))

rs = df.reset_index().set_index('index')
self.assert_(isinstance(rs.index, PeriodIndex))
self.assert_(rs.index.equals(rng))

def test_nested_dict_frame_constructor(self):
rng = period_range('1/1/2000', periods=5)
df = DataFrame(randn(10, 5), columns=rng)
Expand Down

0 comments on commit eb00827

Please sign in to comment.