-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
Separate tests specific to tslibs modules #18036
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5822ad9
move tests specific to tslibs.parsing
jbrockmendel de7d8de
whitespace cleanup
jbrockmendel a99cd6f
edit per reviewer suggestion
jbrockmendel 7965cab
fixup remove extra assert
jbrockmendel 215bc06
Fix test broken by change to tm.assert_numpy_array_equal
jbrockmendel a78600a
remove test class duplicated in test_misc
jbrockmendel beca092
flake8 fixup
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Tests for Timestamp parsing, aimed at pandas/_libs/tslibs/parsing.pyx | ||
""" | ||
from datetime import datetime | ||
|
||
import numpy as np | ||
import pytest | ||
from dateutil.parser import parse | ||
|
||
from pandas import compat | ||
from pandas.util import testing as tm | ||
|
||
from pandas._libs.tslibs import parsing | ||
|
||
|
||
class TestDatetimeParsingWrappers(object): | ||
def test_does_not_convert_mixed_integer(self): | ||
bad_date_strings = ('-50000', '999', '123.1234', 'm', 'T') | ||
|
||
for bad_date_string in bad_date_strings: | ||
assert not parsing._does_string_look_like_datetime(bad_date_string) | ||
|
||
good_date_strings = ('2012-01-01', | ||
'01/01/2012', | ||
'Mon Sep 16, 2013', | ||
'01012012', | ||
'0101', | ||
'1-1') | ||
|
||
for good_date_string in good_date_strings: | ||
assert parsing._does_string_look_like_datetime(good_date_string) | ||
|
||
def test_parsers_quarterly_with_freq(self): | ||
msg = ('Incorrect quarterly string is given, quarter ' | ||
'must be between 1 and 4: 2013Q5') | ||
with tm.assert_raises_regex(parsing.DateParseError, msg): | ||
parsing.parse_time_string('2013Q5') | ||
|
||
# GH 5418 | ||
msg = ('Unable to retrieve month information from given freq: ' | ||
'INVLD-L-DEC-SAT') | ||
with tm.assert_raises_regex(parsing.DateParseError, msg): | ||
parsing.parse_time_string('2013Q1', freq='INVLD-L-DEC-SAT') | ||
|
||
cases = {('2013Q2', None): datetime(2013, 4, 1), | ||
('2013Q2', 'A-APR'): datetime(2012, 8, 1), | ||
('2013-Q2', 'A-DEC'): datetime(2013, 4, 1)} | ||
|
||
for (date_str, freq), exp in compat.iteritems(cases): | ||
result, _, _ = parsing.parse_time_string(date_str, freq=freq) | ||
assert result == exp | ||
|
||
def test_parsers_quarter_invalid(self): | ||
|
||
cases = ['2Q 2005', '2Q-200A', '2Q-200', '22Q2005', '6Q-20', '2Q200.'] | ||
for case in cases: | ||
pytest.raises(ValueError, parsing.parse_time_string, case) | ||
|
||
def test_parsers_monthfreq(self): | ||
cases = {'201101': datetime(2011, 1, 1, 0, 0), | ||
'200005': datetime(2000, 5, 1, 0, 0)} | ||
|
||
for date_str, expected in compat.iteritems(cases): | ||
result1, _, _ = parsing.parse_time_string(date_str, freq='M') | ||
assert result1 == expected | ||
|
||
|
||
class TestGuessDatetimeFormat(object): | ||
def test_guess_datetime_format_with_parseable_formats(self): | ||
tm._skip_if_not_us_locale() | ||
dt_string_to_format = (('20111230', '%Y%m%d'), | ||
('2011-12-30', '%Y-%m-%d'), | ||
('30-12-2011', '%d-%m-%Y'), | ||
('2011-12-30 00:00:00', '%Y-%m-%d %H:%M:%S'), | ||
('2011-12-30T00:00:00', '%Y-%m-%dT%H:%M:%S'), | ||
('2011-12-30 00:00:00.000000', | ||
'%Y-%m-%d %H:%M:%S.%f'), ) | ||
|
||
for dt_string, dt_format in dt_string_to_format: | ||
assert parsing._guess_datetime_format(dt_string) == dt_format | ||
|
||
def test_guess_datetime_format_with_dayfirst(self): | ||
ambiguous_string = '01/01/2011' | ||
assert parsing._guess_datetime_format( | ||
ambiguous_string, dayfirst=True) == '%d/%m/%Y' | ||
assert parsing._guess_datetime_format( | ||
ambiguous_string, dayfirst=False) == '%m/%d/%Y' | ||
|
||
def test_guess_datetime_format_with_locale_specific_formats(self): | ||
# The month names will vary depending on the locale, in which | ||
# case these wont be parsed properly (dateutil can't parse them) | ||
tm._skip_if_has_locale() | ||
|
||
dt_string_to_format = (('30/Dec/2011', '%d/%b/%Y'), | ||
('30/December/2011', '%d/%B/%Y'), | ||
('30/Dec/2011 00:00:00', '%d/%b/%Y %H:%M:%S'), ) | ||
|
||
for dt_string, dt_format in dt_string_to_format: | ||
assert parsing._guess_datetime_format(dt_string) == dt_format | ||
|
||
def test_guess_datetime_format_invalid_inputs(self): | ||
# A datetime string must include a year, month and a day for it | ||
# to be guessable, in addition to being a string that looks like | ||
# a datetime | ||
invalid_dts = [ | ||
'2013', | ||
'01/2013', | ||
'12:00:00', | ||
'1/1/1/1', | ||
'this_is_not_a_datetime', | ||
'51a', | ||
9, | ||
datetime(2011, 1, 1), | ||
] | ||
|
||
for invalid_dt in invalid_dts: | ||
assert parsing._guess_datetime_format(invalid_dt) is None | ||
|
||
def test_guess_datetime_format_nopadding(self): | ||
# GH 11142 | ||
dt_string_to_format = (('2011-1-1', '%Y-%m-%d'), | ||
('30-1-2011', '%d-%m-%Y'), | ||
('1/1/2011', '%m/%d/%Y'), | ||
('2011-1-1 00:00:00', '%Y-%m-%d %H:%M:%S'), | ||
('2011-1-1 0:0:0', '%Y-%m-%d %H:%M:%S'), | ||
('2011-1-3T00:00:0', '%Y-%m-%dT%H:%M:%S')) | ||
|
||
for dt_string, dt_format in dt_string_to_format: | ||
assert parsing._guess_datetime_format(dt_string) == dt_format | ||
|
||
|
||
class TestArrayToDatetime(object): | ||
def test_try_parse_dates(self): | ||
arr = np.array(['5/1/2000', '6/1/2000', '7/1/2000'], dtype=object) | ||
|
||
result = parsing.try_parse_dates(arr, dayfirst=True) | ||
expected = [parse(d, dayfirst=True) for d in arr] | ||
assert tm.assert_numpy_array_equal(result, expected) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Remove the
assert
keyword.tm.assert_...
takes care of that for you. As written, this will cause anAssertionError
since the function call returnsNone
.