Skip to content

Commit 92c15ae

Browse files
silenced many warnings in test suite
1 parent f4acbf9 commit 92c15ae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+220
-103
lines changed

pandas/core/algorithms.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@
5353
)
5454
from pandas.core.dtypes.missing import isna, na_value_for_dtype
5555

56-
from pandas.core.construction import array, extract_array
56+
from pandas.core.construction import (
57+
array,
58+
create_series_with_explicit_index,
59+
extract_array,
60+
)
5761
from pandas.core.indexers import validate_indices
5862

5963
if TYPE_CHECKING:
@@ -835,7 +839,7 @@ def mode(values, dropna: bool = True) -> "Series":
835839
warn(f"Unable to sort modes: {err}")
836840

837841
result = _reconstruct_data(result, original.dtype, original)
838-
return Series(result)
842+
return create_series_with_explicit_index(result)
839843

840844

841845
def rank(

pandas/core/apply.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
)
1717
from pandas.core.dtypes.generic import ABCSeries
1818

19-
from pandas.core.construction import create_series_with_explicit_dtype
19+
from pandas.core.construction import (
20+
create_series_with_explicit_dtype,
21+
create_series_with_explicit_index,
22+
)
2023

2124
if TYPE_CHECKING:
2225
from pandas import DataFrame, Series, Index
@@ -202,15 +205,15 @@ def apply_empty_result(self):
202205

203206
if not should_reduce:
204207
try:
205-
r = self.f(Series([], dtype=np.float64))
208+
r = self.f(create_series_with_explicit_index([], dtype=np.float64))
206209
except Exception:
207210
pass
208211
else:
209212
should_reduce = not isinstance(r, Series)
210213

211214
if should_reduce:
212215
if len(self.agg_axis):
213-
r = self.f(Series([], dtype=np.float64))
216+
r = self.f(create_series_with_explicit_index([], dtype=np.float64))
214217
else:
215218
r = np.nan
216219

pandas/core/construction.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,9 @@ def create_series_with_explicit_index(
674674
if index_if_empty is None:
675675
index_if_empty = Index([])
676676

677-
if index is None and isinstance(data, list) and len(data) == 0:
677+
# dict's are handled separately in Series.__init__
678+
is_relevant_type = is_list_like(data) and not isinstance(data, dict)
679+
if index is None and is_relevant_type and len(data) == 0:
678680
index = index_if_empty
679681
return Series(
680682
data=data, index=index, dtype=dtype, name=name, copy=copy, fastpath=fastpath

pandas/core/indexes/base.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
from pandas.core.arrays.datetimes import tz_to_dtype, validate_tz_from_dtype
7272
from pandas.core.base import IndexOpsMixin, PandasObject
7373
import pandas.core.common as com
74+
from pandas.core.construction import create_series_with_explicit_index
7475
from pandas.core.indexers import deprecate_ndim_indexing
7576
from pandas.core.indexes.frozen import FrozenList
7677
import pandas.core.missing as missing
@@ -142,9 +143,7 @@ def index_arithmetic_method(self, other):
142143
if isinstance(other, (ABCSeries, ABCDataFrame, ABCTimedeltaIndex)):
143144
return NotImplemented
144145

145-
from pandas import Series
146-
147-
result = op(Series(self), other)
146+
result = op(create_series_with_explicit_index(self), other)
148147
if isinstance(result, tuple):
149148
return (Index(result[0]), Index(result[1]))
150149
return Index(result)

pandas/core/strings.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
from pandas.core.algorithms import take_1d
3838
from pandas.core.base import NoNewAttributesMixin
39-
from pandas.core.construction import extract_array
39+
from pandas.core.construction import create_series_with_explicit_index, extract_array
4040

4141
if TYPE_CHECKING:
4242
from pandas.arrays import StringArray
@@ -2180,7 +2180,7 @@ def _wrap_result(
21802180
returns_string=True,
21812181
):
21822182

2183-
from pandas import Index, Series, MultiIndex
2183+
from pandas import Index, MultiIndex
21842184

21852185
# for category, we do the stuff on the categories, so blow it up
21862186
# to the full series again
@@ -2190,7 +2190,9 @@ def _wrap_result(
21902190
if use_codes and self._is_categorical:
21912191
# if self._orig is a CategoricalIndex, there is no .cat-accessor
21922192
result = take_1d(
2193-
result, Series(self._orig, copy=False).cat.codes, fill_value=fill_value
2193+
result,
2194+
create_series_with_explicit_index(self._orig, copy=False).cat.codes,
2195+
fill_value=fill_value,
21942196
)
21952197

21962198
if not hasattr(result, "ndim") or not hasattr(result, "dtype"):

pandas/core/tools/datetimes.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from pandas.core import algorithms
4242
from pandas.core.algorithms import unique
4343
from pandas.core.arrays.datetimes import tz_to_dtype
44+
from pandas.core.construction import create_series_with_explicit_index
4445

4546
# ---------------------------------------------------------------------
4647
# types used in annotations
@@ -764,9 +765,10 @@ def to_datetime(
764765
if errors == "raise":
765766
raise
766767
# ... otherwise, continue without the cache.
767-
from pandas import Series
768768

769-
cache_array = Series([], dtype=object) # just an empty array
769+
cache_array = create_series_with_explicit_index(
770+
[], dtype=object
771+
) # just an empty array
770772
if not cache_array.empty:
771773
result = _convert_and_box_cache(arg, cache_array)
772774
else:

pandas/io/parsers.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@
5353

5454
from pandas.core import algorithms
5555
from pandas.core.arrays import Categorical
56+
from pandas.core.construction import create_series_with_explicit_index
5657
from pandas.core.frame import DataFrame
5758
from pandas.core.indexes.api import (
5859
Index,
5960
MultiIndex,
6061
RangeIndex,
6162
ensure_index_from_sequences,
6263
)
63-
from pandas.core.series import Series
6464
from pandas.core.tools import datetimes as tools
6565

6666
from pandas.io.common import (
@@ -3494,14 +3494,20 @@ def _get_empty_meta(columns, index_col, index_names, dtype=None):
34943494
if (index_col is None or index_col is False) or index_names is None:
34953495
index = Index([])
34963496
else:
3497-
data = [Series([], dtype=dtype[name]) for name in index_names]
3497+
data = [
3498+
create_series_with_explicit_index([], dtype=dtype[name])
3499+
for name in index_names
3500+
]
34983501
index = ensure_index_from_sequences(data, names=index_names)
34993502
index_col.sort()
35003503

35013504
for i, n in enumerate(index_col):
35023505
columns.pop(n - i)
35033506

3504-
col_dict = {col_name: Series([], dtype=dtype[col_name]) for col_name in columns}
3507+
col_dict = {
3508+
col_name: create_series_with_explicit_index([], dtype=dtype[col_name])
3509+
for col_name in columns
3510+
}
35053511

35063512
return index, columns, col_dict
35073513

pandas/io/pytables.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
from pandas.core.arrays import Categorical, DatetimeArray, PeriodArray
5252
import pandas.core.common as com
5353
from pandas.core.computation.pytables import PyTablesExpr, maybe_expression
54+
from pandas.core.construction import create_series_with_explicit_index
5455
from pandas.core.indexes.api import ensure_index
5556

5657
from pandas.io.common import stringify_path
@@ -3313,7 +3314,7 @@ def write_metadata(self, key: str, values: np.ndarray):
33133314
key : str
33143315
values : ndarray
33153316
"""
3316-
values = Series(values)
3317+
values = create_series_with_explicit_index(values)
33173318
self.parent.put(
33183319
self._get_metadata_path(key),
33193320
values,
@@ -4051,7 +4052,9 @@ def read_column(
40514052
encoding=self.encoding,
40524053
errors=self.errors,
40534054
)
4054-
return Series(_set_tz(col_values[1], a.tz), name=column)
4055+
return create_series_with_explicit_index(
4056+
_set_tz(col_values[1], a.tz), name=column
4057+
)
40554058

40564059
raise KeyError(f"column [{column}] not found in the table")
40574060

pandas/tests/arrays/boolean/test_reduction.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pytest
33

44
import pandas as pd
5+
from pandas.core.construction import create_series_with_explicit_index
56

67

78
@pytest.fixture
@@ -31,7 +32,7 @@ def test_any_all(values, exp_any, exp_all, exp_any_noskip, exp_all_noskip):
3132
exp_any_noskip = pd.NA if exp_any_noskip is pd.NA else np.bool_(exp_any_noskip)
3233
exp_all_noskip = pd.NA if exp_all_noskip is pd.NA else np.bool_(exp_all_noskip)
3334

34-
for con in [pd.array, pd.Series]:
35+
for con in [pd.array, create_series_with_explicit_index]:
3536
a = con(values, dtype="boolean")
3637
assert a.any() is exp_any
3738
assert a.all() is exp_all

pandas/tests/arrays/integer/test_function.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pandas as pd
55
import pandas._testing as tm
66
from pandas.core.arrays import integer_array
7+
from pandas.core.construction import create_series_with_explicit_index
78

89

910
@pytest.mark.parametrize("ufunc", [np.abs, np.sign])
@@ -105,7 +106,7 @@ def test_value_counts_na():
105106

106107
def test_value_counts_empty():
107108
# https://github.com/pandas-dev/pandas/issues/33317
108-
s = pd.Series([], dtype="Int64")
109+
s = create_series_with_explicit_index([], dtype="Int64")
109110
result = s.value_counts()
110111
# TODO: The dtype of the index seems wrong (it's int64 for non-empty)
111112
idx = pd.Index([], dtype="object")

pandas/tests/base/test_unique.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import pandas as pd
99
import pandas._testing as tm
10+
from pandas.core.construction import create_series_with_explicit_index
1011
from pandas.tests.base.common import allow_na_ops
1112

1213

@@ -94,7 +95,9 @@ def test_nunique_null(null_obj, index_or_series_obj):
9495
else:
9596
values[0:2] = null_obj
9697

97-
klass = type(obj)
98+
klass = (
99+
create_series_with_explicit_index if isinstance(obj, pd.Series) else type(obj)
100+
)
98101
repeated_values = np.repeat(values, range(1, len(values) + 1))
99102
obj = klass(repeated_values, dtype=obj.dtype)
100103

pandas/tests/base/test_value_counts.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
TimedeltaIndex,
2222
)
2323
import pandas._testing as tm
24+
from pandas.core.construction import create_series_with_explicit_index
2425
from pandas.tests.base.common import allow_na_ops
2526

2627

@@ -180,7 +181,7 @@ def test_value_counts_bins(index_or_series):
180181
assert s.nunique() == 3
181182

182183
s = klass({}) if klass is dict else klass({}, dtype=object)
183-
expected = Series([], dtype=np.int64)
184+
expected = create_series_with_explicit_index([], dtype=np.int64)
184185
tm.assert_series_equal(s.value_counts(), expected, check_index_type=False)
185186
# returned dtype differs depending on original
186187
if isinstance(s, Index):

pandas/tests/dtypes/test_common.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import pandas as pd
2121
import pandas._testing as tm
2222
from pandas.arrays import SparseArray
23+
from pandas.core.construction import create_series_with_explicit_index
2324

2425

2526
# EA & Actual Dtypes
@@ -236,7 +237,9 @@ def test_is_timedelta64_dtype():
236237
assert not com.is_timedelta64_dtype("NO DATE")
237238

238239
assert com.is_timedelta64_dtype(np.timedelta64)
239-
assert com.is_timedelta64_dtype(pd.Series([], dtype="timedelta64[ns]"))
240+
assert com.is_timedelta64_dtype(
241+
create_series_with_explicit_index([], dtype="timedelta64[ns]")
242+
)
240243
assert com.is_timedelta64_dtype(pd.to_timedelta(["0 days", "1 days"]))
241244

242245

@@ -499,7 +502,9 @@ def test_needs_i8_conversion():
499502
assert not com.needs_i8_conversion(np.array(["a", "b"]))
500503

501504
assert com.needs_i8_conversion(np.datetime64)
502-
assert com.needs_i8_conversion(pd.Series([], dtype="timedelta64[ns]"))
505+
assert com.needs_i8_conversion(
506+
create_series_with_explicit_index([], dtype="timedelta64[ns]")
507+
)
503508
assert com.needs_i8_conversion(pd.DatetimeIndex(["2000"], tz="US/Eastern"))
504509

505510

@@ -567,7 +572,7 @@ def test_is_extension_type(check_scipy):
567572
assert com.is_extension_type(pd.DatetimeIndex(["2000"], tz="US/Eastern"))
568573

569574
dtype = DatetimeTZDtype("ns", tz="US/Eastern")
570-
s = pd.Series([], dtype=dtype)
575+
s = create_series_with_explicit_index([], dtype=dtype)
571576
assert com.is_extension_type(s)
572577

573578
if check_scipy:
@@ -596,7 +601,7 @@ def test_is_extension_array_dtype(check_scipy):
596601
assert com.is_extension_array_dtype(pd.DatetimeIndex(["2000"], tz="US/Eastern"))
597602

598603
dtype = DatetimeTZDtype("ns", tz="US/Eastern")
599-
s = pd.Series([], dtype=dtype)
604+
s = create_series_with_explicit_index([], dtype=dtype)
600605
assert com.is_extension_array_dtype(s)
601606

602607
if check_scipy:

pandas/tests/dtypes/test_concat.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pandas as pd
66
from pandas import DatetimeIndex, Period, PeriodIndex, Series, TimedeltaIndex
77
import pandas._testing as tm
8+
from pandas.core.construction import create_series_with_explicit_index
89

910

1011
@pytest.mark.parametrize(
@@ -83,7 +84,7 @@ def test_get_dtype_kinds_period(to_concat, expected):
8384
def test_concat_mismatched_categoricals_with_empty():
8485
# concat_compat behavior on series._values should match pd.concat on series
8586
ser1 = Series(["a", "b", "c"], dtype="category")
86-
ser2 = Series([], dtype="category")
87+
ser2 = create_series_with_explicit_index([], dtype="category")
8788

8889
result = _concat.concat_compat([ser1._values, ser2._values])
8990
expected = pd.concat([ser1, ser2])._values

pandas/tests/dtypes/test_inference.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
)
5353
import pandas._testing as tm
5454
from pandas.core.arrays import IntegerArray
55+
from pandas.core.construction import (
56+
create_series_with_explicit_dtype,
57+
create_series_with_explicit_index,
58+
)
5559

5660

5761
@pytest.fixture(params=[True, False], ids=str)
@@ -77,9 +81,9 @@ def coerce(request):
7781
((x for x in [1, 2]), True, "generator"),
7882
((_ for _ in []), True, "generator-empty"),
7983
(Series([1]), True, "Series"),
80-
(Series([], dtype=object), True, "Series-empty"),
84+
(create_series_with_explicit_dtype([], dtype=object), True, "Series-empty"),
8185
(Series(["a"]).str, True, "StringMethods"),
82-
(Series([], dtype="O").str, True, "StringMethods-empty"),
86+
(create_series_with_explicit_dtype([], dtype="O").str, True, "StringMethods-empty"),
8387
(Index([1]), True, "Index"),
8488
(Index([]), True, "Index-empty"),
8589
(DataFrame([[1]]), True, "DataFrame"),
@@ -138,7 +142,7 @@ def __getitem__(self):
138142

139143

140144
def test_is_array_like():
141-
assert inference.is_array_like(Series([], dtype=object))
145+
assert inference.is_array_like(create_series_with_explicit_index([], dtype=object))
142146
assert inference.is_array_like(Series([1, 2]))
143147
assert inference.is_array_like(np.array(["a", "b"]))
144148
assert inference.is_array_like(Index(["2016-01-01"]))
@@ -164,7 +168,7 @@ class DtypeList(list):
164168
{"a": 1},
165169
{1, "a"},
166170
Series([1]),
167-
Series([], dtype=object),
171+
Series(dtype=object),
168172
Series(["a"]).str,
169173
(x for x in range(5)),
170174
],

pandas/tests/extension/base/missing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pandas as pd
44
import pandas._testing as tm
5+
from pandas.core.construction import create_series_with_explicit_index
56

67
from .base import BaseExtensionTests
78

@@ -19,7 +20,7 @@ def test_isna(self, data_missing):
1920

2021
# GH 21189
2122
result = pd.Series(data_missing).drop([0, 1]).isna()
22-
expected = pd.Series([], dtype=bool)
23+
expected = create_series_with_explicit_index([], dtype=bool)
2324
self.assert_series_equal(result, expected)
2425

2526
def test_dropna_array(self, data_missing):

0 commit comments

Comments
 (0)