Skip to content

Commit fc6f593

Browse files
API: return Index instead of array from datetime field accessors (GH15022)
1 parent 0bf4532 commit fc6f593

File tree

10 files changed

+61
-61
lines changed

10 files changed

+61
-61
lines changed

pandas/tests/indexes/datetimes/test_misc.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -307,5 +307,5 @@ def test_datetimeindex_accessors(self):
307307
def test_nanosecond_field(self):
308308
dti = DatetimeIndex(np.arange(10))
309309

310-
self.assert_numpy_array_equal(dti.nanosecond,
311-
np.arange(10, dtype=np.int32))
310+
self.assert_index_equal(dti.nanosecond,
311+
pd.Index(np.arange(10, dtype=np.int64)))

pandas/tests/indexes/period/test_construction.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ def test_constructor_arrays_negative_year(self):
9191

9292
pindex = PeriodIndex(year=years, quarter=quarters)
9393

94-
self.assert_numpy_array_equal(pindex.year, years)
95-
self.assert_numpy_array_equal(pindex.quarter, quarters)
94+
self.assert_index_equal(pindex.year, pd.Index(years))
95+
self.assert_index_equal(pindex.quarter, pd.Index(quarters))
9696

9797
def test_constructor_invalid_quarters(self):
9898
self.assertRaises(ValueError, PeriodIndex, year=lrange(2000, 2004),

pandas/tests/indexes/period/test_period.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -653,10 +653,10 @@ def test_pindex_fieldaccessor_nat(self):
653653
idx = PeriodIndex(['2011-01', '2011-02', 'NaT',
654654
'2012-03', '2012-04'], freq='D')
655655

656-
exp = np.array([2011, 2011, -1, 2012, 2012], dtype=np.int64)
657-
self.assert_numpy_array_equal(idx.year, exp)
658-
exp = np.array([1, 2, -1, 3, 4], dtype=np.int64)
659-
self.assert_numpy_array_equal(idx.month, exp)
656+
exp = Index([2011, 2011, -1, 2012, 2012], dtype=np.int64)
657+
self.assert_index_equal(idx.year, exp)
658+
exp = Index([1, 2, -1, 3, 4], dtype=np.int64)
659+
self.assert_index_equal(idx.month, exp)
660660

661661
def test_pindex_qaccess(self):
662662
pi = PeriodIndex(['2Q05', '3Q05', '4Q05', '1Q06', '2Q06'], freq='Q')

pandas/tests/tools/test_util.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ def test_datetimeindex(self):
3131
# make sure that the ordering on datetimeindex is consistent
3232
x = date_range('2000-01-01', periods=2)
3333
result1, result2 = [Index(y).day for y in cartesian_product([x, x])]
34-
expected1 = np.array([1, 1, 2, 2], dtype=np.int32)
35-
expected2 = np.array([1, 2, 1, 2], dtype=np.int32)
36-
tm.assert_numpy_array_equal(result1, expected1)
37-
tm.assert_numpy_array_equal(result2, expected2)
34+
expected1 = Index([1, 1, 2, 2])
35+
expected2 = Index([1, 2, 1, 2])
36+
tm.assert_index_equal(result1, expected1)
37+
tm.assert_index_equal(result2, expected2)
3838

3939
def test_empty(self):
4040
# product of empty factors

pandas/tests/tseries/test_timezones.py

+32-38
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,8 @@ def test_field_access_localize(self):
357357
dr = date_range('2011-10-02 00:00', freq='h', periods=10,
358358
tz=self.tzstr('America/Atikokan'))
359359

360-
expected = np.arange(10, dtype=np.int32)
361-
self.assert_numpy_array_equal(dr.hour, expected)
360+
expected = pd.Index(np.arange(10, dtype=np.int64))
361+
self.assert_index_equal(dr.hour, expected)
362362

363363
def test_with_tz(self):
364364
tz = self.tz('US/Central')
@@ -946,35 +946,35 @@ def test_tz_convert_hour_overflow_dst(self):
946946
'2009-05-12 09:50:32']
947947
tt = to_datetime(ts).tz_localize('US/Eastern')
948948
ut = tt.tz_convert('UTC')
949-
expected = np.array([13, 14, 13], dtype=np.int32)
950-
self.assert_numpy_array_equal(ut.hour, expected)
949+
expected = Index([13, 14, 13])
950+
self.assert_index_equal(ut.hour, expected)
951951

952952
# sorted case UTC -> US/Eastern
953953
ts = ['2008-05-12 13:50:00',
954954
'2008-12-12 14:50:35',
955955
'2009-05-12 13:50:32']
956956
tt = to_datetime(ts).tz_localize('UTC')
957957
ut = tt.tz_convert('US/Eastern')
958-
expected = np.array([9, 9, 9], dtype=np.int32)
959-
self.assert_numpy_array_equal(ut.hour, expected)
958+
expected = Index([9, 9, 9])
959+
self.assert_index_equal(ut.hour, expected)
960960

961961
# unsorted case US/Eastern -> UTC
962962
ts = ['2008-05-12 09:50:00',
963963
'2008-12-12 09:50:35',
964964
'2008-05-12 09:50:32']
965965
tt = to_datetime(ts).tz_localize('US/Eastern')
966966
ut = tt.tz_convert('UTC')
967-
expected = np.array([13, 14, 13], dtype=np.int32)
968-
self.assert_numpy_array_equal(ut.hour, expected)
967+
expected = Index([13, 14, 13])
968+
self.assert_index_equal(ut.hour, expected)
969969

970970
# unsorted case UTC -> US/Eastern
971971
ts = ['2008-05-12 13:50:00',
972972
'2008-12-12 14:50:35',
973973
'2008-05-12 13:50:32']
974974
tt = to_datetime(ts).tz_localize('UTC')
975975
ut = tt.tz_convert('US/Eastern')
976-
expected = np.array([9, 9, 9], dtype=np.int32)
977-
self.assert_numpy_array_equal(ut.hour, expected)
976+
expected = Index([9, 9, 9])
977+
self.assert_index_equal(ut.hour, expected)
978978

979979
def test_tz_convert_hour_overflow_dst_timestamps(self):
980980
# Regression test for:
@@ -988,35 +988,35 @@ def test_tz_convert_hour_overflow_dst_timestamps(self):
988988
Timestamp('2009-05-12 09:50:32', tz=tz)]
989989
tt = to_datetime(ts)
990990
ut = tt.tz_convert('UTC')
991-
expected = np.array([13, 14, 13], dtype=np.int32)
992-
self.assert_numpy_array_equal(ut.hour, expected)
991+
expected = Index([13, 14, 13])
992+
self.assert_index_equal(ut.hour, expected)
993993

994994
# sorted case UTC -> US/Eastern
995995
ts = [Timestamp('2008-05-12 13:50:00', tz='UTC'),
996996
Timestamp('2008-12-12 14:50:35', tz='UTC'),
997997
Timestamp('2009-05-12 13:50:32', tz='UTC')]
998998
tt = to_datetime(ts)
999999
ut = tt.tz_convert('US/Eastern')
1000-
expected = np.array([9, 9, 9], dtype=np.int32)
1001-
self.assert_numpy_array_equal(ut.hour, expected)
1000+
expected = Index([9, 9, 9])
1001+
self.assert_index_equal(ut.hour, expected)
10021002

10031003
# unsorted case US/Eastern -> UTC
10041004
ts = [Timestamp('2008-05-12 09:50:00', tz=tz),
10051005
Timestamp('2008-12-12 09:50:35', tz=tz),
10061006
Timestamp('2008-05-12 09:50:32', tz=tz)]
10071007
tt = to_datetime(ts)
10081008
ut = tt.tz_convert('UTC')
1009-
expected = np.array([13, 14, 13], dtype=np.int32)
1010-
self.assert_numpy_array_equal(ut.hour, expected)
1009+
expected = Index([13, 14, 13])
1010+
self.assert_index_equal(ut.hour, expected)
10111011

10121012
# unsorted case UTC -> US/Eastern
10131013
ts = [Timestamp('2008-05-12 13:50:00', tz='UTC'),
10141014
Timestamp('2008-12-12 14:50:35', tz='UTC'),
10151015
Timestamp('2008-05-12 13:50:32', tz='UTC')]
10161016
tt = to_datetime(ts)
10171017
ut = tt.tz_convert('US/Eastern')
1018-
expected = np.array([9, 9, 9], dtype=np.int32)
1019-
self.assert_numpy_array_equal(ut.hour, expected)
1018+
expected = Index([9, 9, 9])
1019+
self.assert_index_equal(ut.hour, expected)
10201020

10211021
def test_tslib_tz_convert_trans_pos_plus_1__bug(self):
10221022
# Regression test for tslib.tz_convert(vals, tz1, tz2).
@@ -1027,9 +1027,8 @@ def test_tslib_tz_convert_trans_pos_plus_1__bug(self):
10271027
idx = idx.tz_localize('UTC')
10281028
idx = idx.tz_convert('Europe/Moscow')
10291029

1030-
expected = np.repeat(np.array([3, 4, 5], dtype=np.int32),
1031-
np.array([n, n, 1]))
1032-
self.assert_numpy_array_equal(idx.hour, expected)
1030+
expected = np.repeat(np.array([3, 4, 5]), np.array([n, n, 1]))
1031+
self.assert_index_equal(idx.hour, Index(expected))
10331032

10341033
def test_tslib_tz_convert_dst(self):
10351034
for freq, n in [('H', 1), ('T', 60), ('S', 3600)]:
@@ -1038,62 +1037,57 @@ def test_tslib_tz_convert_dst(self):
10381037
tz='UTC')
10391038
idx = idx.tz_convert('US/Eastern')
10401039
expected = np.repeat(np.array([18, 19, 20, 21, 22, 23,
1041-
0, 1, 3, 4, 5], dtype=np.int32),
1040+
0, 1, 3, 4, 5]),
10421041
np.array([n, n, n, n, n, n, n, n, n, n, 1]))
1043-
self.assert_numpy_array_equal(idx.hour, expected)
1042+
self.assert_index_equal(idx.hour, Index(expected))
10441043

10451044
idx = date_range('2014-03-08 18:00', '2014-03-09 05:00', freq=freq,
10461045
tz='US/Eastern')
10471046
idx = idx.tz_convert('UTC')
1048-
expected = np.repeat(np.array([23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
1049-
dtype=np.int32),
1047+
expected = np.repeat(np.array([23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
10501048
np.array([n, n, n, n, n, n, n, n, n, n, 1]))
1051-
self.assert_numpy_array_equal(idx.hour, expected)
1049+
self.assert_index_equal(idx.hour, Index(expected))
10521050

10531051
# End DST
10541052
idx = date_range('2014-11-01 23:00', '2014-11-02 09:00', freq=freq,
10551053
tz='UTC')
10561054
idx = idx.tz_convert('US/Eastern')
10571055
expected = np.repeat(np.array([19, 20, 21, 22, 23,
1058-
0, 1, 1, 2, 3, 4], dtype=np.int32),
1056+
0, 1, 1, 2, 3, 4]),
10591057
np.array([n, n, n, n, n, n, n, n, n, n, 1]))
1060-
self.assert_numpy_array_equal(idx.hour, expected)
1058+
self.assert_index_equal(idx.hour, Index(expected))
10611059

10621060
idx = date_range('2014-11-01 18:00', '2014-11-02 05:00', freq=freq,
10631061
tz='US/Eastern')
10641062
idx = idx.tz_convert('UTC')
10651063
expected = np.repeat(np.array([22, 23, 0, 1, 2, 3, 4, 5, 6,
1066-
7, 8, 9, 10], dtype=np.int32),
1064+
7, 8, 9, 10]),
10671065
np.array([n, n, n, n, n, n, n, n, n,
10681066
n, n, n, 1]))
1069-
self.assert_numpy_array_equal(idx.hour, expected)
1067+
self.assert_index_equal(idx.hour, Index(expected))
10701068

10711069
# daily
10721070
# Start DST
10731071
idx = date_range('2014-03-08 00:00', '2014-03-09 00:00', freq='D',
10741072
tz='UTC')
10751073
idx = idx.tz_convert('US/Eastern')
1076-
self.assert_numpy_array_equal(idx.hour,
1077-
np.array([19, 19], dtype=np.int32))
1074+
self.assert_index_equal(idx.hour, Index([19, 19]))
10781075

10791076
idx = date_range('2014-03-08 00:00', '2014-03-09 00:00', freq='D',
10801077
tz='US/Eastern')
10811078
idx = idx.tz_convert('UTC')
1082-
self.assert_numpy_array_equal(idx.hour,
1083-
np.array([5, 5], dtype=np.int32))
1079+
self.assert_index_equal(idx.hour, Index([5, 5]))
10841080

10851081
# End DST
10861082
idx = date_range('2014-11-01 00:00', '2014-11-02 00:00', freq='D',
10871083
tz='UTC')
10881084
idx = idx.tz_convert('US/Eastern')
1089-
self.assert_numpy_array_equal(idx.hour,
1090-
np.array([20, 20], dtype=np.int32))
1085+
self.assert_index_equal(idx.hour, Index([20, 20]))
10911086

10921087
idx = date_range('2014-11-01 00:00', '2014-11-02 000:00', freq='D',
10931088
tz='US/Eastern')
10941089
idx = idx.tz_convert('UTC')
1095-
self.assert_numpy_array_equal(idx.hour,
1096-
np.array([4, 4], dtype=np.int32))
1090+
self.assert_index_equal(idx.hour, Index([4, 4]))
10971091

10981092
def test_tzlocal(self):
10991093
# GH 13583

pandas/tseries/common.py

+2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ def _delegate_property_get(self, name):
106106
elif not is_list_like(result):
107107
return result
108108

109+
result = np.asarray(result)
110+
109111
# blow up if we operate on categories
110112
if self.orig is not None:
111113
result = take_1d(result, self.orig.cat.codes)

pandas/tseries/converter.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ def period_break(dates, period):
455455
"""
456456
current = getattr(dates, period)
457457
previous = getattr(dates - 1, period)
458-
return (current - previous).nonzero()[0]
458+
return np.nonzero(current - previous)[0]
459459

460460

461461
def has_level_label(label_flags, vmin):

pandas/tseries/index.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,19 @@ def f(self):
7777

7878
result = tslib.get_start_end_field(values, field, self.freqstr,
7979
month_kw)
80+
result = self._maybe_mask_results(result, convert='float64')
81+
8082
elif field in ['weekday_name']:
8183
result = tslib.get_date_name_field(values, field)
82-
return self._maybe_mask_results(result)
84+
result = self._maybe_mask_results(result)
8385
elif field in ['is_leap_year']:
8486
# no need to mask NaT
85-
return tslib.get_date_field(values, field)
87+
result = tslib.get_date_field(values, field)
8688
else:
8789
result = tslib.get_date_field(values, field)
90+
result = self._maybe_mask_results(result, convert='float64')
8891

89-
return self._maybe_mask_results(result, convert='float64')
92+
return Index(result)
9093

9194
f.__name__ = name
9295
f.__doc__ = docstring
@@ -1913,9 +1916,9 @@ def to_julian_date(self):
19131916
"""
19141917

19151918
# http://mysite.verizon.net/aesir_research/date/jdalg2.htm
1916-
year = self.year
1917-
month = self.month
1918-
day = self.day
1919+
year = np.asarray(self.year)
1920+
month = np.asarray(self.month)
1921+
day = np.asarray(self.day)
19191922
testarr = month < 3
19201923
year[testarr] -= 1
19211924
month[testarr] += 12

pandas/tseries/period.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
def _field_accessor(name, alias, docstring=None):
5353
def f(self):
5454
base, mult = _gfc(self.freq)
55-
return get_period_field_arr(alias, self._values, base)
55+
result = get_period_field_arr(alias, self._values, base)
56+
return Index(result)
5657
f.__name__ = name
5758
f.__doc__ = docstring
5859
return property(f)
@@ -585,7 +586,7 @@ def to_datetime(self, dayfirst=False):
585586
@property
586587
def is_leap_year(self):
587588
""" Logical indicating if the date belongs to a leap year """
588-
return tslib._isleapyear_arr(self.year)
589+
return tslib._isleapyear_arr(np.asarray(self.year))
589590

590591
@property
591592
def start_time(self):

pandas/tseries/util.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def pivot_annual(series, freq=None):
5454

5555
if freq == 'D':
5656
width = 366
57-
offset = index.dayofyear - 1
57+
offset = np.asarray(index.dayofyear) - 1
5858

5959
# adjust for leap year
6060
offset[(~isleapyear(year)) & (offset >= 59)] += 1
@@ -63,7 +63,7 @@ def pivot_annual(series, freq=None):
6363
# todo: strings like 1/1, 1/25, etc.?
6464
elif freq in ('M', 'BM'):
6565
width = 12
66-
offset = index.month - 1
66+
offset = np.asarray(index.month) - 1
6767
columns = lrange(1, 13)
6868
elif freq == 'H':
6969
width = 8784

0 commit comments

Comments
 (0)