Skip to content

Commit 40f4253

Browse files
reworked some test changes - instead of asserting the warning, the dtype is specified explicitely mostly
1 parent efb84c9 commit 40f4253

24 files changed

+61
-110
lines changed

pandas/core/generic.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -6281,6 +6281,8 @@ def fillna(
62816281
2 NaN 1.0 NaN 5
62826282
3 NaN 3.0 NaN 4
62836283
"""
6284+
from pandas import Series
6285+
62846286
inplace = validate_bool_kwarg(inplace, "inplace")
62856287
value, method = validate_fillna_kwargs(value, method)
62866288

@@ -6317,8 +6319,10 @@ def fillna(
63176319
return self
63186320

63196321
if self.ndim == 1:
6320-
if isinstance(value, (dict, ABCSeries)):
6321-
from pandas import Series
6322+
if isinstance(value, dict):
6323+
dtype = object if not value else None
6324+
value = Series(value, dtype=dtype)
6325+
elif isinstance(value, ABCSeries):
63226326

63236327
value = Series(value)
63246328
elif not is_list_like(value):

pandas/io/html.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,8 @@ def _parse_tfoot_tr(self, table):
767767

768768

769769
def _expand_elements(body):
770-
lens = Series([len(elem) for elem in body])
770+
dtype = None if body else object
771+
lens = Series([len(elem) for elem in body], dtype=dtype)
771772
lens_max = lens.max()
772773
not_max = lens[lens != lens_max]
773774

pandas/tests/io/json/test_pandas.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ def setup(self, datapath):
5353
self.objSeries = tm.makeObjectSeries()
5454
self.objSeries.name = "objects"
5555

56-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
57-
self.empty_series = Series([], index=[])
56+
self.empty_series = Series([], index=[], dtype=np.float64)
5857
self.empty_frame = DataFrame()
5958

6059
self.frame = _frame.copy()

pandas/tests/io/test_html.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,7 @@ def test_empty_tables(self):
415415
</tbody>
416416
</table>
417417
"""
418-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
419-
result = self.read_html(html)
420-
418+
result = self.read_html(html)
421419
assert len(result) == 1
422420

423421
def test_multiple_tbody(self):

pandas/tests/resample/test_base.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ def test_resample_empty_dataframe(empty_frame, freq, resample_method):
123123
expected = df.copy()
124124
else:
125125
# GH14962
126-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
127-
expected = Series([])
126+
expected = Series([], dtype=object)
128127

129128
if isinstance(df.index, PeriodIndex):
130129
expected.index = df.index.asfreq(freq=freq)

pandas/tests/reshape/test_concat.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -2227,11 +2227,8 @@ def test_concat_empty_series_timelike(self, tz, values):
22272227
# GH 18447
22282228

22292229
first = Series([], dtype="M8[ns]").dt.tz_localize(tz)
2230-
if values:
2231-
second = Series(values)
2232-
else:
2233-
with tm.assert_produces_warning(FutureWarning):
2234-
second = Series(values)
2230+
dtype = None if values else np.float64
2231+
second = Series(values, dtype=dtype)
22352232

22362233
expected = DataFrame(
22372234
{
@@ -2592,11 +2589,7 @@ def test_concat_odered_dict(self):
25922589
@pytest.mark.parametrize("dt", np.sctypes["float"])
25932590
def test_concat_no_unnecessary_upcast(dt, pdt):
25942591
# GH 13247
2595-
if pdt is Series:
2596-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
2597-
dims = pdt().ndim
2598-
else:
2599-
dims = pdt().ndim
2592+
dims = pdt(dtype=object).ndim
26002593

26012594
dfs = [
26022595
pdt(np.array([1], dtype=dt, ndmin=dims)),
@@ -2633,8 +2626,7 @@ def test_concat_empty_and_non_empty_frame_regression():
26332626
def test_concat_empty_and_non_empty_series_regression():
26342627
# GH 18187 regression test
26352628
s1 = pd.Series([1])
2636-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
2637-
s2 = pd.Series([])
2629+
s2 = pd.Series([], dtype=object)
26382630

26392631
expected = s1
26402632
result = pd.concat([s1, s2])

pandas/tests/series/indexing/test_alter_index.py

+5-10
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,7 @@ def test_reindex_with_datetimes():
230230

231231
def test_reindex_corner(datetime_series):
232232
# (don't forget to fix this) I think it's fixed
233-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
234-
empty = Series()
233+
empty = Series(dtype=object)
235234
empty.reindex(datetime_series.index, method="pad") # it works
236235

237236
# corner case: pad empty series
@@ -540,9 +539,8 @@ def test_drop_with_ignore_errors():
540539
def test_drop_empty_list(index, drop_labels):
541540
# GH 21494
542541
expected_index = [i for i in index if i not in drop_labels]
543-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
544-
series = pd.Series(index=index).drop(drop_labels)
545-
expected = pd.Series(index=expected_index)
542+
series = pd.Series(index=index, dtype=object).drop(drop_labels)
543+
expected = pd.Series(index=expected_index, dtype=object)
546544
tm.assert_series_equal(series, expected)
547545

548546

@@ -557,8 +555,5 @@ def test_drop_empty_list(index, drop_labels):
557555
def test_drop_non_empty_list(data, index, drop_labels):
558556
# GH 21494 and GH 16877
559557
with pytest.raises(KeyError, match="not found in axis"):
560-
if data is None:
561-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
562-
pd.Series(data=data, index=index).drop(drop_labels)
563-
else:
564-
pd.Series(data=data, index=index).drop(drop_labels)
558+
dtype = object if data is None else None
559+
pd.Series(data=data, index=index, dtype=dtype).drop(drop_labels)

pandas/tests/series/indexing/test_datetime.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ def test_series_set_value():
105105
dates = [datetime(2001, 1, 1), datetime(2001, 1, 2)]
106106
index = DatetimeIndex(dates)
107107

108-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
109-
s = Series()._set_value(dates[0], 1.0)
108+
s = Series(dtype=object)._set_value(dates[0], 1.0)
110109
s2 = s._set_value(dates[1], np.nan)
111110

112111
expected = Series([1.0, np.nan], index=index)

pandas/tests/series/indexing/test_indexing.py

+7-13
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,8 @@ def test_getitem_get(datetime_series, string_series, object_series):
109109

110110
# None
111111
# GH 5652
112-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
113-
s1 = Series()
114-
s2 = Series(index=list("abc"))
112+
s1 = Series(dtype=object)
113+
s2 = Series(dtype=object, index=list("abc"))
115114
for s in [s1, s2]:
116115
result = s.get(None)
117116
assert result is None
@@ -137,8 +136,7 @@ def test_getitem_generator(string_series):
137136

138137
def test_type_promotion():
139138
# GH12599
140-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
141-
s = pd.Series()
139+
s = pd.Series(dtype=object)
142140
s["a"] = pd.Timestamp("2016-01-01")
143141
s["b"] = 3.0
144142
s["c"] = "foo"
@@ -176,8 +174,7 @@ def test_getitem_out_of_bounds(datetime_series):
176174
datetime_series[len(datetime_series)]
177175

178176
# GH #917
179-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
180-
s = Series([])
177+
s = Series([], dtype=object)
181178
with pytest.raises(IndexError, match=msg):
182179
s[-1]
183180

@@ -334,14 +331,12 @@ def test_setitem(datetime_series, string_series):
334331

335332
# Test for issue #10193
336333
key = pd.Timestamp("2012-01-01")
337-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
338-
series = pd.Series()
334+
series = pd.Series(dtype=object)
339335
series[key] = 47
340336
expected = pd.Series(47, [key])
341337
tm.assert_series_equal(series, expected)
342338

343-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
344-
series = pd.Series([], pd.DatetimeIndex([], freq="D"))
339+
series = pd.Series([], pd.DatetimeIndex([], freq="D"), dtype=object)
345340
series[key] = 47
346341
expected = pd.Series(47, pd.DatetimeIndex([key], freq="D"))
347342
tm.assert_series_equal(series, expected)
@@ -649,8 +644,7 @@ def test_setitem_na():
649644

650645
def test_timedelta_assignment():
651646
# GH 8209
652-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
653-
s = Series([])
647+
s = Series([], dtype=object)
654648
s.loc["B"] = timedelta(1)
655649
tm.assert_series_equal(s, Series(Timedelta("1 days"), index=["B"]))
656650

pandas/tests/series/indexing/test_numeric.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ def test_delitem():
153153
tm.assert_series_equal(s, expected)
154154

155155
# empty
156-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
157-
s = Series()
156+
s = Series(dtype=object)
158157

159158
with pytest.raises(KeyError, match=r"^0$"):
160159
del s[0]

pandas/tests/series/test_api.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,7 @@ def get_dir(s):
289289
)
290290
def test_index_tab_completion(self, index):
291291
# dir contains string-like values of the Index.
292-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
293-
s = pd.Series(index=index)
292+
s = pd.Series(index=index, dtype=object)
294293
dir_s = dir(s)
295294
for i, x in enumerate(s.index.unique(level=0)):
296295
if i < 100:
@@ -299,8 +298,7 @@ def test_index_tab_completion(self, index):
299298
assert x not in dir_s
300299

301300
def test_not_hashable(self):
302-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
303-
s_empty = Series()
301+
s_empty = Series(dtype=object)
304302
s = Series([1])
305303
msg = "'Series' objects are mutable, thus they cannot be hashed"
306304
with pytest.raises(TypeError, match=msg):
@@ -499,12 +497,10 @@ def test_str_attribute(self):
499497
s.str.repeat(2)
500498

501499
def test_empty_method(self):
502-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
503-
s_empty = pd.Series()
500+
s_empty = pd.Series(dtype=object)
504501
assert s_empty.empty
505502

506-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
507-
s2 = pd.Series(index=[1])
503+
s2 = pd.Series(index=[1], dtype=object)
508504
for full_series in [pd.Series([1]), s2]:
509505
assert not full_series.empty
510506

pandas/tests/series/test_apply.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ def test_apply(self, datetime_series):
3636
assert s.name == rs.name
3737

3838
# index but no data
39-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
40-
s = Series(index=[1, 2, 3])
39+
s = Series(index=[1, 2, 3], dtype=np.float64)
4140
rs = s.apply(lambda x: x)
4241
tm.assert_series_equal(s, rs)
4342

pandas/tests/series/test_combine_concat.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ def test_combine_first(self):
107107

108108
# corner case
109109
s = Series([1.0, 2, 3], index=[0, 1, 2])
110-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
111-
empty = Series([], index=[])
110+
empty = Series([], index=[], dtype=object)
112111
result = s.combine_first(empty)
113112
s.index = s.index.astype("O")
114113
tm.assert_series_equal(s, result)

pandas/tests/series/test_dtypes.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,7 @@ def test_astype_dict_like(self, dtype_class):
225225
# GH16717
226226
# if dtypes provided is empty, it should error
227227
if dtype_class is Series:
228-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
229-
dt5 = dtype_class({})
228+
dt5 = dtype_class({}, dtype=object)
230229
else:
231230
dt5 = dtype_class({})
232231

pandas/tests/series/test_duplicates.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,9 @@ def test_unique_data_ownership():
7070
)
7171
def test_is_unique(data, expected):
7272
# GH11946 / GH25180
73-
if isinstance(data, list) and not data:
74-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
75-
s = Series(data)
76-
else:
77-
s = Series(data)
73+
is_empty = isinstance(data, list) and not data
74+
dtype = object if is_empty else None
75+
s = Series(data, dtype=dtype)
7876
assert s.is_unique is expected
7977

8078

pandas/tests/series/test_explode.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ def test_mixed_type():
2929

3030

3131
def test_empty():
32-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
33-
s = pd.Series()
32+
s = pd.Series(dtype=object)
3433
result = s.explode()
3534
expected = s.copy()
3635
tm.assert_series_equal(result, expected)

pandas/tests/series/test_missing.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -714,11 +714,9 @@ def test_fillna(self, datetime_series):
714714
result = s1.fillna(s2)
715715
expected = Series([1.0])
716716
tm.assert_series_equal(result, expected)
717-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
718-
result = s1.fillna({})
717+
result = s1.fillna({})
719718
tm.assert_series_equal(result, s1)
720-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
721-
result = s1.fillna(Series(()))
719+
result = s1.fillna(Series((), dtype=object))
722720
tm.assert_series_equal(result, s1)
723721
result = s2.fillna(s1)
724722
tm.assert_series_equal(result, s2)
@@ -842,8 +840,7 @@ def test_timedelta64_nan(self):
842840
# tm.assert_series_equal(selector, expected)
843841

844842
def test_dropna_empty(self):
845-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
846-
s = Series([])
843+
s = Series([], dtype=object)
847844

848845
assert len(s.dropna()) == 0
849846
s.dropna(inplace=True)
@@ -1173,8 +1170,7 @@ def test_interpolate_corners(self, kwargs):
11731170
s = Series([np.nan, np.nan])
11741171
tm.assert_series_equal(s.interpolate(**kwargs), s)
11751172

1176-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1177-
s = Series([]).interpolate()
1173+
s = Series([], dtype=object).interpolate()
11781174
tm.assert_series_equal(s.interpolate(**kwargs), s)
11791175

11801176
def test_interpolate_index_values(self):

pandas/tests/series/test_operators.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ def test_logical_operators_bool_dtype_with_empty(self):
3333

3434
s_tft = Series([True, False, True], index=index)
3535
s_fff = Series([False, False, False], index=index)
36-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
37-
s_empty = Series([])
36+
s_empty = Series([], dtype=object)
3837

3938
res = s_tft & s_empty
4039
expected = s_fff
@@ -409,8 +408,7 @@ def test_logical_ops_label_based(self):
409408
# filling
410409

411410
# vs empty
412-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
413-
empty = Series([])
411+
empty = Series([], dtype=object)
414412

415413
result = a & empty.copy()
416414
expected = Series([False, False, False], list("bca"))
@@ -801,8 +799,7 @@ def test_ops_datetimelike_align(self):
801799
tm.assert_series_equal(result, expected)
802800

803801
def test_operators_corner(self, datetime_series):
804-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
805-
empty = Series([], index=Index([]))
802+
empty = Series([], index=Index([]), dtype=np.float64)
806803

807804
result = datetime_series + empty
808805
assert np.isnan(result).all()

pandas/tests/series/test_quantile.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ def test_quantile_nan(self):
104104
assert result == expected
105105

106106
# all nan/empty
107-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
108-
s1 = Series([])
107+
s1 = Series([], dtype=object)
109108
cases = [s1, Series([np.nan, np.nan])]
110109

111110
for s in cases:

pandas/tests/series/test_repr.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ def test_name_printing(self):
6262
s.name = None
6363
assert "Name:" not in repr(s)
6464

65-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
66-
s = Series(index=date_range("20010101", "20020101"), name="test")
65+
s = Series(index=date_range("20010101", "20020101"), name="test", dtype=object)
6766
assert "Name: test" in repr(s)
6867

6968
def test_repr(self, datetime_series, string_series, object_series):
@@ -76,8 +75,7 @@ def test_repr(self, datetime_series, string_series, object_series):
7675
str(Series(tm.randn(1000), index=np.arange(1000, 0, step=-1)))
7776

7877
# empty
79-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
80-
str(Series())
78+
str(Series(dtype=object))
8179

8280
# with NaNs
8381
string_series[5:7] = np.NaN

0 commit comments

Comments
 (0)