Skip to content

Commit d1cdfd2

Browse files
author
MarcoGorelli
committed
✅ add/update tests
1 parent ab78002 commit d1cdfd2

File tree

14 files changed

+138
-204
lines changed

14 files changed

+138
-204
lines changed

pandas/tests/apply/test_frame_apply.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,8 @@ def test_with_dictlike_columns_with_datetime():
836836
df["author"] = ["X", "Y", "Z"]
837837
df["publisher"] = ["BBC", "NBC", "N24"]
838838
df["date"] = pd.to_datetime(
839-
["17-10-2010 07:15:30", "13-05-2011 08:20:35", "15-01-2013 09:09:09"]
839+
["17-10-2010 07:15:30", "13-05-2011 08:20:35", "15-01-2013 09:09:09"],
840+
dayfirst=True,
840841
)
841842
result = df.apply(lambda x: {}, axis=1)
842843
expected = Series([{}, {}, {}])

pandas/tests/frame/methods/test_drop.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,11 @@ def test_drop_level_nonunique_datetime(self):
405405
idx = Index([2, 3, 4, 4, 5], name="id")
406406
idxdt = pd.to_datetime(
407407
[
408-
"201603231400",
409-
"201603231500",
410-
"201603231600",
411-
"201603231600",
412-
"201603231700",
408+
"2016-03-23 14:00",
409+
"2016-03-23 15:00",
410+
"2016-03-23 16:00",
411+
"2016-03-23 16:00",
412+
"2016-03-23 17:00",
413413
]
414414
)
415415
df = DataFrame(np.arange(10).reshape(5, 2), columns=list("ab"), index=idx)

pandas/tests/frame/methods/test_to_csv.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
class TestDataFrameToCSV:
2929
def read_csv(self, path, **kwargs):
30-
params = {"index_col": 0, "parse_dates": True}
30+
params = {"index_col": 0}
3131
params.update(**kwargs)
3232

3333
return read_csv(path, **params)
@@ -46,17 +46,17 @@ def test_to_csv_from_csv1(self, float_frame, datetime_frame):
4646
# freq does not roundtrip
4747
datetime_frame.index = datetime_frame.index._with_freq(None)
4848
datetime_frame.to_csv(path)
49-
recons = self.read_csv(path)
49+
recons = self.read_csv(path, parse_dates=True)
5050
tm.assert_frame_equal(datetime_frame, recons)
5151

5252
datetime_frame.to_csv(path, index_label="index")
53-
recons = self.read_csv(path, index_col=None)
53+
recons = self.read_csv(path, index_col=None, parse_dates=True)
5454

5555
assert len(recons.columns) == len(datetime_frame.columns) + 1
5656

5757
# no index
5858
datetime_frame.to_csv(path, index=False)
59-
recons = self.read_csv(path, index_col=None)
59+
recons = self.read_csv(path, index_col=None, parse_dates=True)
6060
tm.assert_almost_equal(datetime_frame.values, recons.values)
6161

6262
# corner case
@@ -1056,7 +1056,7 @@ def test_to_csv_date_format(self, datetime_frame):
10561056

10571057
# test NaTs
10581058
nat_index = to_datetime(
1059-
["NaT"] * 10 + ["2000-01-01", "1/1/2000", "1-1-2000"]
1059+
["NaT"] * 10 + ["2000-01-01", "2000-01-01", "2000-01-01"]
10601060
)
10611061
nat_frame = DataFrame({"A": nat_index}, index=nat_index)
10621062
nat_frame.to_csv(path, date_format="%Y-%m-%d")

pandas/tests/indexes/datetimes/test_constructors.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -1042,10 +1042,18 @@ def test_datetimeindex_constructor_misc(self):
10421042
arr = np.array(["1/1/2005", "1/2/2005", "1/3/2005", "2005-01-04"], dtype="O")
10431043
idx4 = DatetimeIndex(arr)
10441044

1045-
arr = to_datetime(["1/1/2005", "1/2/2005", "1/3/2005", "2005-01-04"])
1045+
# Can't be parsed consistently, need to parse each element individually
1046+
arr = [
1047+
to_datetime(date_string)
1048+
for date_string in ["1/1/2005", "1/2/2005", "1/3/2005", "2005-01-04"]
1049+
]
10461050
idx5 = DatetimeIndex(arr)
10471051

1048-
arr = to_datetime(["1/1/2005", "1/2/2005", "Jan 3, 2005", "2005-01-04"])
1052+
# Can't be parsed consistently, need to parse each element individually
1053+
arr = [
1054+
to_datetime(date_string)
1055+
for date_string in ["1/1/2005", "1/2/2005", "Jan 3, 2005", "2005-01-04"]
1056+
]
10491057
idx6 = DatetimeIndex(arr)
10501058

10511059
idx7 = DatetimeIndex(["12/05/2007", "25/01/2008"], dayfirst=True)

pandas/tests/indexes/test_base.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -1185,10 +1185,16 @@ def test_equals_op_index_vs_mi_same_length(self):
11851185
expected = np.array([False, False, False])
11861186
tm.assert_numpy_array_equal(result, expected)
11871187

1188-
@pytest.mark.parametrize("dt_conv", [pd.to_datetime, pd.to_timedelta])
1189-
def test_dt_conversion_preserves_name(self, dt_conv):
1188+
@pytest.mark.parametrize(
1189+
"dt_conv, arg",
1190+
[
1191+
(pd.to_datetime, ["2000-01-01", "2000-01-02"]),
1192+
(pd.to_timedelta, ["01:02:03", "01:02:04"]),
1193+
],
1194+
)
1195+
def test_dt_conversion_preserves_name(self, dt_conv, arg):
11901196
# GH 10875
1191-
index = Index(["01:02:03", "01:02:04"], name="label")
1197+
index = Index(arg, name="label")
11921198
assert index.name == dt_conv(index).name
11931199

11941200
def test_cached_properties_not_settable(self):

pandas/tests/io/parser/common/test_common_basic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ def _set_noconvert_columns(self):
5858
return CParserWrapper._set_noconvert_columns(self)
5959

6060
data = """a,b,c,d,e
61-
0,1,20140101,0900,4
62-
0,1,20140102,1000,4"""
61+
0,1,2014-01-01,09:00,4
62+
0,1,2014-01-02,10:00,4"""
6363

6464
parse_dates = [[1, 2]]
6565
cols = {

pandas/tests/io/parser/test_parse_dates.py

+29-72
Original file line numberDiff line numberDiff line change
@@ -1666,9 +1666,9 @@ def test_parse_delimited_date_swap_no_warning(
16661666
@pytest.mark.parametrize(
16671667
"date_string,dayfirst,expected",
16681668
[
1669-
# %d/%m/%Y; month > 12 thus replacement
1669+
# %d/%m/%Y; month > 12
16701670
("13/02/2019", False, datetime(2019, 2, 13)),
1671-
# %m/%d/%Y; day > 12 thus there will be no replacement
1671+
# %m/%d/%Y; day > 12
16721672
("02/13/2019", True, datetime(2019, 2, 13)),
16731673
],
16741674
)
@@ -1677,7 +1677,10 @@ def test_parse_delimited_date_swap_with_warning(
16771677
):
16781678
parser = all_parsers
16791679
expected = DataFrame({0: [expected]}, dtype="datetime64[ns]")
1680-
warning_msg = "Specify a format to ensure consistent parsing"
1680+
warning_msg = (
1681+
"Parsing dates in .* format when dayfirst=.* was specified. "
1682+
"Pass `dayfirst=.*` or specify a format to silence this warning."
1683+
)
16811684
result = parser.read_csv_check_warnings(
16821685
UserWarning,
16831686
warning_msg,
@@ -1691,13 +1694,11 @@ def test_parse_delimited_date_swap_with_warning(
16911694

16921695
def test_parse_multiple_delimited_dates_with_swap_warnings():
16931696
# GH46210
1694-
warning_msg = "Specify a format to ensure consistent parsing"
1695-
with tm.assert_produces_warning(UserWarning, match=warning_msg) as record:
1697+
with pytest.raises(
1698+
ValueError,
1699+
match=r"^time data '31/05/2000' does not match format '%m/%d/%Y' \(match\)$",
1700+
):
16961701
pd.to_datetime(["01/01/2000", "31/05/2000", "31/05/2001", "01/02/2000"])
1697-
assert len({str(warning.message) for warning in record}) == 1
1698-
# Using set(record) as repetitions of the same warning are suppressed
1699-
# https://docs.python.org/3/library/warnings.html
1700-
# and here we care to check that the warning is only shows once to users.
17011702

17021703

17031704
def _helper_hypothesis_delimited_date(call, date_string, **kwargs):
@@ -1860,97 +1861,51 @@ def test_parse_dates_and_keep_orgin_column(all_parsers):
18601861

18611862
def test_dayfirst_warnings():
18621863
# GH 12585
1863-
warning_msg_day_first = (
1864-
r"Parsing dates in DD/MM/YYYY format when dayfirst=False \(the default\) was "
1865-
r"specified. This may lead to inconsistently parsed dates! Specify a format "
1866-
r"to ensure consistent parsing."
1867-
)
1868-
warning_msg_month_first = (
1869-
"Parsing dates in MM/DD/YYYY format when dayfirst=True was "
1870-
"specified. This may lead to inconsistently parsed dates! Specify a format "
1871-
"to ensure consistent parsing."
1872-
)
18731864

18741865
# CASE 1: valid input
18751866
input = "date\n31/12/2014\n10/03/2011"
1876-
expected_consistent = DatetimeIndex(
1867+
expected = DatetimeIndex(
18771868
["2014-12-31", "2011-03-10"], dtype="datetime64[ns]", freq=None, name="date"
18781869
)
1879-
expected_inconsistent = DatetimeIndex(
1880-
["2014-12-31", "2011-10-03"], dtype="datetime64[ns]", freq=None, name="date"
1870+
warning_msg = (
1871+
"Parsing dates in .* format when dayfirst=.* was specified. "
1872+
"Pass `dayfirst=.*` or specify a format to silence this warning."
18811873
)
18821874

18831875
# A. dayfirst arg correct, no warning
18841876
res1 = read_csv(
18851877
StringIO(input), parse_dates=["date"], dayfirst=True, index_col="date"
18861878
).index
1887-
tm.assert_index_equal(expected_consistent, res1)
1879+
tm.assert_index_equal(expected, res1)
18881880

1889-
# B. dayfirst arg incorrect, warning + incorrect output
1890-
with tm.assert_produces_warning(UserWarning, match=warning_msg_day_first):
1881+
# B. dayfirst arg incorrect, warning
1882+
with tm.assert_produces_warning(UserWarning, match=warning_msg):
18911883
res2 = read_csv(
18921884
StringIO(input), parse_dates=["date"], dayfirst=False, index_col="date"
18931885
).index
1894-
tm.assert_index_equal(expected_inconsistent, res2)
1895-
1896-
# C. dayfirst default arg, same as B
1897-
with tm.assert_produces_warning(UserWarning, match=warning_msg_day_first):
1898-
res3 = read_csv(
1899-
StringIO(input), parse_dates=["date"], dayfirst=False, index_col="date"
1900-
).index
1901-
tm.assert_index_equal(expected_inconsistent, res3)
1902-
1903-
# D. infer_datetime_format=True overrides dayfirst default
1904-
# no warning + correct result
1905-
res4 = read_csv(
1906-
StringIO(input),
1907-
parse_dates=["date"],
1908-
infer_datetime_format=True,
1909-
index_col="date",
1910-
).index
1911-
tm.assert_index_equal(expected_consistent, res4)
1886+
tm.assert_index_equal(expected, res2)
19121887

19131888
# CASE 2: invalid input
19141889
# cannot consistently process with single format
1915-
# warnings *always* raised
1890+
# return to user unaltered
19161891

19171892
# first in DD/MM/YYYY, second in MM/DD/YYYY
19181893
input = "date\n31/12/2014\n03/30/2011"
1919-
expected = DatetimeIndex(
1920-
["2014-12-31", "2011-03-30"], dtype="datetime64[ns]", freq=None, name="date"
1921-
)
1894+
expected = Index(["31/12/2014", "03/30/2011"], dtype="object", name="date")
19221895

19231896
# A. use dayfirst=True
1924-
with tm.assert_produces_warning(UserWarning, match=warning_msg_month_first):
1925-
res5 = read_csv(
1926-
StringIO(input), parse_dates=["date"], dayfirst=True, index_col="date"
1927-
).index
1897+
res5 = read_csv(
1898+
StringIO(input), parse_dates=["date"], dayfirst=True, index_col="date"
1899+
).index
19281900
tm.assert_index_equal(expected, res5)
19291901

19301902
# B. use dayfirst=False
1931-
with tm.assert_produces_warning(UserWarning, match=warning_msg_day_first):
1903+
with tm.assert_produces_warning(UserWarning, match=warning_msg):
19321904
res6 = read_csv(
19331905
StringIO(input), parse_dates=["date"], dayfirst=False, index_col="date"
19341906
).index
19351907
tm.assert_index_equal(expected, res6)
19361908

1937-
# C. use dayfirst default arg, same as B
1938-
with tm.assert_produces_warning(UserWarning, match=warning_msg_day_first):
1939-
res7 = read_csv(
1940-
StringIO(input), parse_dates=["date"], dayfirst=False, index_col="date"
1941-
).index
1942-
tm.assert_index_equal(expected, res7)
1943-
1944-
# D. use infer_datetime_format=True
1945-
with tm.assert_produces_warning(UserWarning, match=warning_msg_day_first):
1946-
res8 = read_csv(
1947-
StringIO(input),
1948-
parse_dates=["date"],
1949-
infer_datetime_format=True,
1950-
index_col="date",
1951-
).index
1952-
tm.assert_index_equal(expected, res8)
1953-
19541909

19551910
@pytest.mark.parametrize(
19561911
"date_string, dayfirst",
@@ -1973,9 +1928,11 @@ def test_dayfirst_warnings_no_leading_zero(date_string, dayfirst):
19731928
expected = DatetimeIndex(
19741929
["2014-01-31"], dtype="datetime64[ns]", freq=None, name="date"
19751930
)
1976-
with tm.assert_produces_warning(
1977-
UserWarning, match=r"may lead to inconsistently parsed dates"
1978-
):
1931+
warning_msg = (
1932+
"Parsing dates in .* format when dayfirst=.* was specified. "
1933+
"Pass `dayfirst=.*` or specify a format to silence this warning."
1934+
)
1935+
with tm.assert_produces_warning(UserWarning, match=warning_msg):
19791936
res = read_csv(
19801937
StringIO(initial_value),
19811938
parse_dates=["date"],

pandas/tests/io/parser/usecols/test_parse_dates.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
def test_usecols_with_parse_dates(all_parsers, usecols):
3232
# see gh-9755
3333
data = """a,b,c,d,e
34-
0,1,20140101,0900,4
35-
0,1,20140102,1000,4"""
34+
0,1,2014-01-01,09:00,4
35+
0,1,2014-01-02,10:00,4"""
3636
parser = all_parsers
3737
parse_dates = [[1, 2]]
3838

@@ -138,8 +138,8 @@ def test_usecols_with_parse_dates4(all_parsers):
138138
)
139139
def test_usecols_with_parse_dates_and_names(all_parsers, usecols, names):
140140
# see gh-9755
141-
s = """0,1,20140101,0900,4
142-
0,1,20140102,1000,4"""
141+
s = """0,1,2014-01-01,09:00,4
142+
0,1,2014-01-02,10:00,4"""
143143
parse_dates = [[1, 2]]
144144
parser = all_parsers
145145

pandas/tests/io/test_sql.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,7 @@ def test_sqlalchemy_type_mapping(self):
13861386

13871387
# Test Timestamp objects (no datetime64 because of timezone) (GH9085)
13881388
df = DataFrame(
1389-
{"time": to_datetime(["201412120154", "201412110254"], utc=True)}
1389+
{"time": to_datetime(["2014-12-12 01:54", "2014-12-11 02:54"], utc=True)}
13901390
)
13911391
db = sql.SQLDatabase(self.conn)
13921392
table = sql.SQLTable("test_type", db, frame=df)
@@ -1595,7 +1595,7 @@ def test_sqlite_type_mapping(self):
15951595

15961596
# Test Timestamp objects (no datetime64 because of timezone) (GH9085)
15971597
df = DataFrame(
1598-
{"time": to_datetime(["201412120154", "201412110254"], utc=True)}
1598+
{"time": to_datetime(["2014-12-12 01:54", "2014-12-11 02:54"], utc=True)}
15991599
)
16001600
db = sql.SQLiteDatabase(self.conn)
16011601
table = sql.SQLiteTable("test_type", db, frame=df)

pandas/tests/io/xml/test_xml_dtypes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ def test_day_first_parse_dates(parser):
457457
)
458458

459459
with tm.assert_produces_warning(
460-
UserWarning, match="Parsing dates in DD/MM/YYYY format"
460+
UserWarning, match="Parsing dates in %d/%m/%Y format"
461461
):
462462
df_result = read_xml(xml, parse_dates=["date"], parser=parser)
463463
df_iter = read_xml_iterparse(

pandas/tests/plotting/test_converter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ def dtc(self):
161161
return converter.DatetimeConverter()
162162

163163
def test_convert_accepts_unicode(self, dtc):
164-
r1 = dtc.convert("12:22", None, None)
165-
r2 = dtc.convert("12:22", None, None)
164+
r1 = dtc.convert("2000-01-01 12:22", None, None)
165+
r2 = dtc.convert("2000-01-01 12:22", None, None)
166166
assert r1 == r2, "DatetimeConverter.convert should accept unicode"
167167

168168
def test_conversion(self, dtc):

pandas/tests/series/methods/test_to_csv.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
class TestSeriesToCSV:
1515
def read_csv(self, path, **kwargs):
16-
params = {"index_col": 0, "header": None, "parse_dates": True}
16+
params = {"index_col": 0, "header": None}
1717
params.update(**kwargs)
1818

1919
header = params.get("header")
@@ -30,7 +30,7 @@ def test_from_csv(self, datetime_series, string_series):
3030

3131
with tm.ensure_clean() as path:
3232
datetime_series.to_csv(path, header=False)
33-
ts = self.read_csv(path)
33+
ts = self.read_csv(path, parse_dates=True)
3434
tm.assert_series_equal(datetime_series, ts, check_names=False)
3535

3636
assert ts.name is None
@@ -55,7 +55,7 @@ def test_from_csv(self, datetime_series, string_series):
5555
with open(path, "w") as outfile:
5656
outfile.write("1998-01-01|1.0\n1999-01-01|2.0")
5757

58-
series = self.read_csv(path, sep="|")
58+
series = self.read_csv(path, sep="|", parse_dates=True)
5959
check_series = Series(
6060
{datetime(1998, 1, 1): 1.0, datetime(1999, 1, 1): 2.0}
6161
)

0 commit comments

Comments
 (0)