-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
TST: Tests and Helpers for Datetime/Period Arrays #23502
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
083135d
b282e8f
1e56627
5e1b3ef
d1188ac
caedb95
5a02923
cd060d1
baad2af
87070a2
72a849b
03f5208
59b7064
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
Tests for DatetimeArray | ||
""" | ||
import operator | ||
|
||
import numpy as np | ||
|
||
import pandas as pd | ||
from pandas.core.arrays import DatetimeArrayMixin as DatetimeArray | ||
import pandas.util.testing as tm | ||
|
||
|
||
class TestDatetimeArrayComparisons(object): | ||
# TODO: merge this into tests/arithmetic/test_datetime64 once it is | ||
# sufficiently robust | ||
|
||
def test_cmp_dt64_arraylike_tznaive(self, all_compare_operators): | ||
# arbitrary tz-naive DatetimeIndex | ||
opname = all_compare_operators.strip('_') | ||
op = getattr(operator, opname) | ||
|
||
dti = pd.date_range('2016-01-1', freq='MS', periods=9, tz=None) | ||
arr = DatetimeArray(dti) | ||
assert arr.freq == dti.freq | ||
assert arr.tz == dti.tz | ||
|
||
right = dti | ||
|
||
expected = np.ones(len(arr), dtype=bool) | ||
if opname in ['ne', 'gt', 'lt']: | ||
# for these the comparisons should be all-False | ||
expected = ~expected | ||
|
||
result = op(arr, arr) | ||
tm.assert_numpy_array_equal(result, expected) | ||
for other in [right, np.array(right)]: | ||
# TODO: add list and tuple, and object-dtype once those | ||
# are fixed in the constructor | ||
result = op(arr, other) | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
result = op(other, arr) | ||
tm.assert_numpy_array_equal(result, expected) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,9 @@ | |
IntervalIndex, MultiIndex, Panel, PeriodIndex, RangeIndex, Series, | ||
TimedeltaIndex, bdate_range) | ||
from pandas.core.algorithms import take_1d | ||
from pandas.core.arrays import ExtensionArray, IntervalArray, PeriodArray | ||
from pandas.core.arrays import ( | ||
DatetimeArrayMixin as DatetimeArray, ExtensionArray, IntervalArray, | ||
PeriodArray, period_array) | ||
import pandas.core.common as com | ||
|
||
from pandas.io.common import urlopen | ||
|
@@ -1049,6 +1051,15 @@ def assert_period_array_equal(left, right, obj='PeriodArray'): | |
assert_attr_equal('freq', left, right, obj=obj) | ||
|
||
|
||
def assert_datetime_array_equal(left, right, obj='DatetimeArray'): | ||
_check_isinstance(left, right, DatetimeArray) | ||
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. why is this needed? why don’t we just have a more generic assert_array_equal adding more comparison routines generally is just asking for trouble - we already have too many 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 think we may be able to de-duplicate these once DTA/TDA are EAs, but for now the options are to have this function or to implement something equivalent inside 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 think it is fine to reconsider this once all internal ExtensionArrays are in place (I think the same was said when adding the 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 really like having separate ones, so that you can assert that you aren't accidentally comparing two of the wrong kind of object. If you just have a generic 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. again not sure why this is; the point is we are comparing versus an expected value eg we recently consolidatednto assert_equal to avoid this exact prome sure it’s slightly more explicit but IMHO is not worth the extra functions and maintenance over time 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.
sometimes though, that expected value isn't constructed explicitly. Sometimes it's the result of some computation (think of the tests in ops for example).
tm.assert_period_array_equal(a, b) than tm.assert_array_equal(a, b, kind='period') or, worse, So that's my case, but I'm more than happy to be out voted here :) 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. it’s fine just want to reduce maintenance burden |
||
|
||
assert_numpy_array_equal(left._data, right._data, | ||
obj='{obj}._data'.format(obj=obj)) | ||
assert_attr_equal('freq', left, right, obj=obj) | ||
assert_attr_equal('tz', left, right, obj=obj) | ||
|
||
|
||
def raise_assert_detail(obj, message, left, right, diff=None): | ||
__tracebackhide__ = True | ||
|
||
|
@@ -1546,6 +1557,8 @@ def assert_equal(left, right, **kwargs): | |
assert_interval_array_equal(left, right, **kwargs) | ||
elif isinstance(left, PeriodArray): | ||
assert_period_array_equal(left, right, **kwargs) | ||
elif isinstance(left, DatetimeArray): | ||
assert_datetime_array_equal(left, right, **kwargs) | ||
elif isinstance(left, ExtensionArray): | ||
assert_extension_array_equal(left, right, **kwargs) | ||
elif isinstance(left, np.ndarray): | ||
|
@@ -1573,6 +1586,11 @@ def box_expected(expected, box_cls): | |
expected = pd.Series(expected) | ||
elif box_cls is pd.DataFrame: | ||
expected = pd.Series(expected).to_frame() | ||
elif box_cls is PeriodArray: | ||
# the PeriodArray constructor is not as flexible as period_array | ||
expected = period_array(expected) | ||
elif box_cls is DatetimeArray: | ||
expected = DatetimeArray(expected) | ||
elif box_cls is np.ndarray: | ||
expected = np.array(expected) | ||
else: | ||
|
Uh oh!
There was an error while loading. Please reload this page.