Skip to content

Commit 41008c7

Browse files
API: return Index instead of array from datetime field accessors (GH15022)
1 parent fb7af6e commit 41008c7

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
@@ -313,5 +313,5 @@ def test_datetimeindex_accessors(self):
313313
def test_nanosecond_field(self):
314314
dti = DatetimeIndex(np.arange(10))
315315

316-
self.assert_numpy_array_equal(dti.nanosecond,
317-
np.arange(10, dtype=np.int32))
316+
self.assert_index_equal(dti.nanosecond,
317+
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
@@ -660,10 +660,10 @@ def test_pindex_fieldaccessor_nat(self):
660660
idx = PeriodIndex(['2011-01', '2011-02', 'NaT',
661661
'2012-03', '2012-04'], freq='D')
662662

663-
exp = np.array([2011, 2011, -1, 2012, 2012], dtype=np.int64)
664-
self.assert_numpy_array_equal(idx.year, exp)
665-
exp = np.array([1, 2, -1, 3, 4], dtype=np.int64)
666-
self.assert_numpy_array_equal(idx.month, exp)
663+
exp = Index([2011, 2011, -1, 2012, 2012], dtype=np.int64)
664+
self.assert_index_equal(idx.year, exp)
665+
exp = Index([1, 2, -1, 3, 4], dtype=np.int64)
666+
self.assert_index_equal(idx.month, exp)
667667

668668
def test_pindex_qaccess(self):
669669
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
@@ -358,8 +358,8 @@ def test_field_access_localize(self):
358358
dr = date_range('2011-10-02 00:00', freq='h', periods=10,
359359
tz=self.tzstr('America/Atikokan'))
360360

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

10991093
def test_tzlocal(self):
11001094
# GH 13583

pandas/tseries/common.py

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

108+
result = np.asarray(result)
109+
108110
# blow up if we operate on categories
109111
if self.orig is not None:
110112
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
@@ -73,16 +73,19 @@ def f(self):
7373

7474
result = libts.get_start_end_field(values, field, self.freqstr,
7575
month_kw)
76+
result = self._maybe_mask_results(result, convert='float64')
77+
7678
elif field in ['weekday_name']:
7779
result = libts.get_date_name_field(values, field)
78-
return self._maybe_mask_results(result)
80+
result = self._maybe_mask_results(result)
7981
elif field in ['is_leap_year']:
8082
# no need to mask NaT
81-
return libts.get_date_field(values, field)
83+
result = libts.get_date_field(values, field)
8284
else:
8385
result = libts.get_date_field(values, field)
86+
result = self._maybe_mask_results(result, convert='float64')
8487

85-
return self._maybe_mask_results(result, convert='float64')
88+
return Index(result)
8689

8790
f.__name__ = name
8891
f.__doc__ = docstring
@@ -1909,9 +1912,9 @@ def to_julian_date(self):
19091912
"""
19101913

19111914
# http://mysite.verizon.net/aesir_research/date/jdalg2.htm
1912-
year = self.year
1913-
month = self.month
1914-
day = self.day
1915+
year = np.asarray(self.year)
1916+
month = np.asarray(self.month)
1917+
day = np.asarray(self.day)
19151918
testarr = month < 3
19161919
year[testarr] -= 1
19171920
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)