-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
API: return Index instead of array from DatetimeIndex field accessors (GH15022) #15589
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
Changes from all commits
41008c7
52f9008
f2831e2
cdf6cae
96ed069
6317b6b
41728a9
ffacd38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -424,7 +424,7 @@ def test_total_seconds(self): | |
freq='s') | ||
expt = [1 * 86400 + 10 * 3600 + 11 * 60 + 12 + 100123456. / 1e9, | ||
1 * 86400 + 10 * 3600 + 11 * 60 + 13 + 100123456. / 1e9] | ||
tm.assert_almost_equal(rng.total_seconds(), np.array(expt)) | ||
tm.assert_almost_equal(rng.total_seconds(), Index(expt)) | ||
|
||
# test Series | ||
s = Series(rng) | ||
|
@@ -486,16 +486,16 @@ def test_append_numpy_bug_1681(self): | |
def test_fields(self): | ||
rng = timedelta_range('1 days, 10:11:12.100123456', periods=2, | ||
freq='s') | ||
self.assert_numpy_array_equal(rng.days, np.array( | ||
[1, 1], dtype='int64')) | ||
self.assert_numpy_array_equal( | ||
self.assert_index_equal(rng.days, Index([1, 1], dtype='int64')) | ||
self.assert_index_equal( | ||
rng.seconds, | ||
np.array([10 * 3600 + 11 * 60 + 12, 10 * 3600 + 11 * 60 + 13], | ||
dtype='int64')) | ||
self.assert_numpy_array_equal(rng.microseconds, np.array( | ||
[100 * 1000 + 123, 100 * 1000 + 123], dtype='int64')) | ||
self.assert_numpy_array_equal(rng.nanoseconds, np.array( | ||
[456, 456], dtype='int64')) | ||
Index([10 * 3600 + 11 * 60 + 12, 10 * 3600 + 11 * 60 + 13], | ||
dtype='int64')) | ||
self.assert_index_equal( | ||
rng.microseconds, | ||
Index([100 * 1000 + 123, 100 * 1000 + 123], dtype='int64')) | ||
self.assert_index_equal(rng.nanoseconds, | ||
Index([456, 456], dtype='int64')) | ||
|
||
self.assertRaises(AttributeError, lambda: rng.hours) | ||
self.assertRaises(AttributeError, lambda: rng.minutes) | ||
|
@@ -509,6 +509,10 @@ def test_fields(self): | |
tm.assert_series_equal(s.dt.seconds, Series( | ||
[10 * 3600 + 11 * 60 + 12, np.nan], index=[0, 1])) | ||
|
||
# preserve name (GH15589) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might be better to add something to pandas/tests/indexes/datetimelike.py. These are inherited by all of the datetimelike test indexes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only problem is that they don't have a common field attribute. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I now ensured I have a test for each of period, timedelta, datetime that checks the name preservation, but indeed, ideally would have a test in datetimelike.py for that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
you should simply run it for but no big deal There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that would indeed be a possibility, and just checked and eg also There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah prob should just define these as fixtures I think, then would make it really easy |
||
rng.name = 'name' | ||
assert rng.days.name == 'name' | ||
|
||
def test_freq_conversion(self): | ||
|
||
# doc example | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -597,9 +597,20 @@ def test_nat_fields(self): | |
def test_nat_vector_field_access(self): | ||
idx = DatetimeIndex(['1/1/2000', None, None, '1/4/2000']) | ||
|
||
# non boolean fields | ||
fields = ['year', 'quarter', 'month', 'day', 'hour', 'minute', | ||
'second', 'microsecond', 'nanosecond', 'week', 'dayofyear', | ||
'days_in_month', 'is_leap_year'] | ||
'days_in_month'] | ||
|
||
for field in fields: | ||
result = getattr(idx, field) | ||
expected = [getattr(x, field) for x in idx] | ||
self.assert_index_equal(result, pd.Index(expected)) | ||
|
||
# boolean fields | ||
fields = ['is_leap_year'] | ||
# other boolean fields like 'is_month_start' and 'is_month_end' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose let's make an issue for this NaT enhancement? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, will open an issue for that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
# not yet supported by NaT | ||
|
||
for field in fields: | ||
result = getattr(idx, field) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a ref here