-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
isolate scalar Timestamp tests from date_range tests #17957
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 3 commits
b5687b0
0d1f801
ad43be6
01e9b77
059cba0
f35e59f
1f0f982
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 |
---|---|---|
|
@@ -969,35 +969,6 @@ def test_delta_preserve_nanos(self): | |
result = val + timedelta(1) | ||
assert result.nanosecond == val.nanosecond | ||
|
||
def test_frequency_misc(self): | ||
assert (frequencies.get_freq_group('T') == | ||
frequencies.FreqGroup.FR_MIN) | ||
|
||
code, stride = frequencies.get_freq_code(offsets.Hour()) | ||
assert code == frequencies.FreqGroup.FR_HR | ||
|
||
code, stride = frequencies.get_freq_code((5, 'T')) | ||
assert code == frequencies.FreqGroup.FR_MIN | ||
assert stride == 5 | ||
|
||
offset = offsets.Hour() | ||
result = frequencies.to_offset(offset) | ||
assert result == offset | ||
|
||
result = frequencies.to_offset((5, 'T')) | ||
expected = offsets.Minute(5) | ||
assert result == expected | ||
|
||
pytest.raises(ValueError, frequencies.get_freq_code, (5, 'baz')) | ||
|
||
pytest.raises(ValueError, frequencies.to_offset, '100foo') | ||
|
||
pytest.raises(ValueError, frequencies.to_offset, ('', '')) | ||
|
||
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): | ||
result = frequencies.get_standard_freq(offsets.Hour()) | ||
assert result == 'H' | ||
|
||
def test_hash_equivalent(self): | ||
d = {datetime(2011, 1, 1): 5} | ||
stamp = Timestamp(datetime(2011, 1, 1)) | ||
|
@@ -1268,26 +1239,19 @@ def test_compare_hour13(self): | |
class TestTimeSeries(object): | ||
|
||
def test_timestamp_to_datetime(self): | ||
rng = date_range('20090415', '20090519', tz='US/Eastern') | ||
|
||
stamp = rng[0] | ||
stamp = Timestamp('20090415', tz='US/Eastern', freq='D') | ||
dtval = stamp.to_pydatetime() | ||
assert stamp == dtval | ||
assert stamp.tzinfo == dtval.tzinfo | ||
|
||
def test_timestamp_to_datetime_dateutil(self): | ||
rng = date_range('20090415', '20090519', tz='dateutil/US/Eastern') | ||
|
||
stamp = rng[0] | ||
stamp = Timestamp('20090415', tz='dateutil/US/Eastern', freq='D') | ||
dtval = stamp.to_pydatetime() | ||
assert stamp == dtval | ||
assert stamp.tzinfo == dtval.tzinfo | ||
|
||
def test_timestamp_to_datetime_explicit_pytz(self): | ||
rng = date_range('20090415', '20090519', | ||
tz=pytz.timezone('US/Eastern')) | ||
|
||
stamp = rng[0] | ||
stamp = Timestamp('20090415', tz=pytz.timezone('US/Eastern'), freq='D') | ||
dtval = stamp.to_pydatetime() | ||
assert stamp == dtval | ||
assert stamp.tzinfo == dtval.tzinfo | ||
|
@@ -1296,9 +1260,7 @@ def test_timestamp_to_datetime_explicit_dateutil(self): | |
tm._skip_if_windows_python_3() | ||
|
||
from pandas._libs.tslibs.timezones import dateutil_gettz as gettz | ||
rng = date_range('20090415', '20090519', tz=gettz('US/Eastern')) | ||
|
||
stamp = rng[0] | ||
stamp = Timestamp('20090415', tz=gettz('US/Eastern'), freq='D') | ||
dtval = stamp.to_pydatetime() | ||
assert stamp == dtval | ||
assert stamp.tzinfo == dtval.tzinfo | ||
|
@@ -1494,3 +1456,61 @@ def test_to_datetime_bijective(self): | |
with tm.assert_produces_warning(exp_warning, check_stacklevel=False): | ||
assert (Timestamp(Timestamp.min.to_pydatetime()).value / 1000 == | ||
Timestamp.min.value / 1000) | ||
|
||
|
||
class TestTimestampEquivDateRange(object): | ||
# Older tests in TestTimeSeries constructed their `stamp` objects | ||
# using `date_range` instead of the `Timestamp` constructor. | ||
# TestTimestampEquivDateRange checks that these are equivalent in the | ||
# pertinent cases. | ||
|
||
def test_date_range_timestamp_equiv(self): | ||
rng = date_range('20090415', '20090519', tz='US/Eastern') | ||
stamp = rng[0] | ||
|
||
ts = Timestamp('20090415', tz='US/Eastern', freq='D') | ||
assert ts == stamp | ||
|
||
def test_date_range_timestamp_equiv_dateutil(self): | ||
rng = date_range('20090415', '20090519', tz='dateutil/US/Eastern') | ||
stamp = rng[0] | ||
|
||
ts = Timestamp('20090415', tz='dateutil/US/Eastern', freq='D') | ||
assert ts == stamp | ||
|
||
def test_date_range_timestamp_equiv_explicit_pytz(self): | ||
rng = date_range('20090415', '20090519', | ||
tz=pytz.timezone('US/Eastern')) | ||
stamp = rng[0] | ||
|
||
ts = Timestamp('20090415', tz=pytz.timezone('US/Eastern'), freq='D') | ||
assert ts == stamp | ||
|
||
def test_date_range_timestamp_equiv_explicit_dateutil(self): | ||
tm._skip_if_windows_python_3() | ||
from pandas._libs.tslibs.timezones import dateutil_gettz as gettz | ||
|
||
rng = date_range('20090415', '20090519', tz=gettz('US/Eastern')) | ||
stamp = rng[0] | ||
|
||
ts = Timestamp('20090415', tz=gettz('US/Eastern'), freq='D') | ||
assert ts == stamp | ||
|
||
def test_date_range_timestamp_equiv_from_datetime_instance(self): | ||
# This test refers to TestTimestampOps.test_addition_subtraction_types | ||
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 sure if comments like these are necessary. 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. Will remove. |
||
datetime_instance = datetime(2014, 3, 4) | ||
# build a timestamp with a frequency, since then it supports | ||
# addition/subtraction of integers | ||
timestamp_instance = date_range(datetime_instance, periods=1, | ||
freq='D')[0] | ||
|
||
ts = Timestamp(datetime_instance, freq='D') | ||
assert ts == timestamp_instance | ||
|
||
def test_date_range_timestamp_equiv_preserve_frequency(self): | ||
# This test refers to | ||
# TestTimestampOps.test_addition_subtraction_preserve_frequency | ||
timestamp_instance = date_range('2014-03-05', periods=1, freq='D')[0] | ||
ts = Timestamp('2014-03-05', freq='D') | ||
|
||
assert timestamp_instance == ts |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -509,6 +509,35 @@ def test_get_freq_code(self): | |
assert (frequencies.get_freq_code(offsets.Week(-2, weekday=4)) == | ||
(frequencies.get_freq('W-FRI'), -2)) | ||
|
||
def test_frequency_misc(self): | ||
assert (frequencies.get_freq_group('T') == | ||
frequencies.FreqGroup.FR_MIN) | ||
|
||
code, stride = frequencies.get_freq_code(offsets.Hour()) | ||
assert code == frequencies.FreqGroup.FR_HR | ||
|
||
code, stride = frequencies.get_freq_code((5, 'T')) | ||
assert code == frequencies.FreqGroup.FR_MIN | ||
assert stride == 5 | ||
|
||
offset = offsets.Hour() | ||
result = frequencies.to_offset(offset) | ||
assert result == offset | ||
|
||
result = frequencies.to_offset((5, 'T')) | ||
expected = offsets.Minute(5) | ||
assert result == expected | ||
|
||
pytest.raises(ValueError, frequencies.get_freq_code, (5, 'baz')) | ||
|
||
pytest.raises(ValueError, frequencies.to_offset, '100foo') | ||
|
||
pytest.raises(ValueError, frequencies.to_offset, ('', '')) | ||
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. What are the error messages when you make these method calls? I would consider using 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. No idea. This is just cut/paste from test_timestamp. 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. Gotcha. Run these lines of code and see what error messages you get. Then we can see whether we should check the message in the test. 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. Done, pushing updated... |
||
|
||
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): | ||
result = frequencies.get_standard_freq(offsets.Hour()) | ||
assert result == 'H' | ||
|
||
|
||
_dti = DatetimeIndex | ||
|
||
|
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.
Given that you use this construct several times across tests (i.e.
rng = date_range(...)[0]
), you might want to consider making that a separate method.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.
Given the large number of irons in the fire at the moment, would you be OK with these comments going into #17652 and being addressed in a follow-up?
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.
@jbrockmendel : I would prefer to make a push to refactor now, as this is a reorganization PR after all. Save it in the end as a last commit to this PR.
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.
Alright, let's wrap this thing up! Thanks for taking the time to review.
Seems like replacing a 1-line function call with a 1-line method call doesn't really gain anything, no?