Skip to content

Commit

Permalink
Preserve name for DatetimeIndex field accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche committed Mar 7, 2017
1 parent 76d1564 commit 45b838a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
30 changes: 12 additions & 18 deletions pandas/tests/indexes/datetimes/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,24 +230,18 @@ def test_datetimeindex_accessors(self):
self.assertEqual(Timestamp('2016-04-09').weekday_name, u'Saturday')
self.assertEqual(Timestamp('2016-04-10').weekday_name, u'Sunday')

self.assertEqual(len(dti.year), 365)
self.assertEqual(len(dti.month), 365)
self.assertEqual(len(dti.day), 365)
self.assertEqual(len(dti.hour), 365)
self.assertEqual(len(dti.minute), 365)
self.assertEqual(len(dti.second), 365)
self.assertEqual(len(dti.microsecond), 365)
self.assertEqual(len(dti.dayofweek), 365)
self.assertEqual(len(dti.dayofyear), 365)
self.assertEqual(len(dti.weekofyear), 365)
self.assertEqual(len(dti.quarter), 365)
self.assertEqual(len(dti.is_month_start), 365)
self.assertEqual(len(dti.is_month_end), 365)
self.assertEqual(len(dti.is_quarter_start), 365)
self.assertEqual(len(dti.is_quarter_end), 365)
self.assertEqual(len(dti.is_year_start), 365)
self.assertEqual(len(dti.is_year_end), 365)
self.assertEqual(len(dti.weekday_name), 365)
dti.name = 'name'

for accessor in ['year', 'month', 'day', 'hour', 'minute', 'second',
'microsecond', 'nanosecond', 'dayofweek', 'dayofyear',
'weekofyear', 'quarter',
'is_month_start', 'is_month_end',
'is_quarter_start', 'is_quarter_end',
'is_year_start', 'is_year_end', 'weekday_name']:
res = getattr(dti, accessor)
self.assertEqual(len(res), 365)
self.assertIsInstance(res, Index)
self.assertEqual(res.name, 'name')

dti = DatetimeIndex(freq='BQ-FEB', start=datetime(1998, 1, 1),
periods=4)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tseries/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def f(self):
result = tslib.get_date_field(values, field)
result = self._maybe_mask_results(result, convert='float64')

return Index(result)
return Index(result, name=self.name)

f.__name__ = name
f.__doc__ = docstring
Expand Down

0 comments on commit 45b838a

Please sign in to comment.