Skip to content

Commit 3eb2271

Browse files
committed
asserted FutureWarning in relevant tests
1 parent fe341cf commit 3eb2271

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

pandas/_libs/tslibs/timestamps.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2592,7 +2592,7 @@ class Timestamp(_Timestamp):
25922592
"Passing an empty string to Timestamp is deprecated and will raise "
25932593
"a ValueError in a future version.",
25942594
FutureWarning,
2595-
stacklevel = 2
2595+
stacklevel = find_stack_level()
25962596
)
25972597
25982598
# User passed a date string to parse.

pandas/tests/indexing/test_loc.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@
3939
to_timedelta,
4040
)
4141
import pandas._testing as tm
42-
from pandas.api.types import is_scalar
42+
from pandas.api.types import (
43+
is_datetime64_any_dtype,
44+
is_scalar,
45+
)
4346
from pandas.core.indexing import _one_ellipsis_message
4447
from pandas.tests.indexing.common import check_indexing_smoketest_or_raises
4548

@@ -2084,7 +2087,21 @@ def test_loc_setitem_with_expansion_nonunique_index(self, index):
20842087
assert key not in index # otherwise test is invalid
20852088
# TODO: using a tuple key breaks here in many cases
20862089

2087-
exp_index = index.insert(len(index), key)
2090+
msg = "Passing an empty string to Timestamp"
2091+
contains_datetime = False
2092+
if isinstance(index, MultiIndex):
2093+
for i in range(index.nlevels):
2094+
if is_datetime64_any_dtype(index.get_level_values(i)):
2095+
contains_datetime = True
2096+
break
2097+
if contains_datetime:
2098+
with tm.assert_produces_warning(FutureWarning, match=msg):
2099+
exp_index = index.insert(len(index), key)
2100+
else:
2101+
exp_index = index.insert(len(index), key)
2102+
else:
2103+
exp_index = index.insert(len(index), key)
2104+
20882105
if isinstance(index, MultiIndex):
20892106
assert exp_index[-1][0] == key
20902107
else:
@@ -2094,19 +2111,32 @@ def test_loc_setitem_with_expansion_nonunique_index(self, index):
20942111

20952112
# Add new row, but no new columns
20962113
df = orig.copy()
2097-
df.loc[key, 0] = N
2114+
if contains_datetime:
2115+
with tm.assert_produces_warning(FutureWarning, match=msg):
2116+
df.loc[key, 0] = N
2117+
else:
2118+
df.loc[key, 0] = N
20982119
tm.assert_frame_equal(df, expected)
20992120

21002121
# add new row on a Series
21012122
ser = orig.copy()[0]
2102-
ser.loc[key] = N
2123+
if contains_datetime:
2124+
with tm.assert_produces_warning(FutureWarning, match=msg):
2125+
ser.loc[key] = N
2126+
else:
2127+
ser.loc[key] = N
2128+
tm.assert_frame_equal(df, expected)
21032129
# the series machinery lets us preserve int dtype instead of float
21042130
expected = expected[0].astype(np.int64)
21052131
tm.assert_series_equal(ser, expected)
21062132

21072133
# add new row and new column
21082134
df = orig.copy()
2109-
df.loc[key, 1] = N
2135+
if contains_datetime:
2136+
with tm.assert_produces_warning(FutureWarning, match=msg):
2137+
df.loc[key, 1] = N
2138+
else:
2139+
df.loc[key, 1] = N
21102140
expected = DataFrame(
21112141
{0: list(arr) + [np.nan], 1: [np.nan] * N + [float(N)]},
21122142
index=exp_index,

pandas/tests/io/formats/test_to_csv.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,15 +302,19 @@ def test_to_csv_date_format_in_categorical(self):
302302
ser = pd.Series(pd.to_datetime(["2021-03-27", pd.NaT], format="%Y-%m-%d"))
303303
ser = ser.astype("category")
304304
expected = tm.convert_rows_list_to_csv_str(["0", "2021-03-27", '""'])
305-
assert ser.to_csv(index=False) == expected
305+
msg = "Passing an empty string to Timestamp"
306+
with tm.assert_produces_warning(FutureWarning, match=msg):
307+
assert ser.to_csv(index=False) == expected
306308

307309
ser = pd.Series(
308310
pd.date_range(
309311
start="2021-03-27", freq="D", periods=1, tz="Europe/Berlin"
310312
).append(pd.DatetimeIndex([pd.NaT]))
311313
)
312314
ser = ser.astype("category")
313-
assert ser.to_csv(index=False, date_format="%Y-%m-%d") == expected
315+
msg = "Passing an empty string to Timestamp"
316+
with tm.assert_produces_warning(FutureWarning, match=msg):
317+
assert ser.to_csv(index=False, date_format="%Y-%m-%d") == expected
314318

315319
def test_to_csv_float_ea_float_format(self):
316320
# GH#45991

pandas/tests/test_multilevel.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,9 @@ def test_multiindex_dt_with_nan(self):
313313
names=[None, "Date"],
314314
),
315315
)
316-
df = df.reset_index()
316+
msg = "Passing an empty string to Timestamp"
317+
with tm.assert_produces_warning(FutureWarning, match=msg):
318+
df = df.reset_index()
317319
result = df[df.columns[0]]
318320
expected = Series(["a", "b", "c", "d"], name=("sub", np.nan))
319321
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)