diff --git a/doc/source/user_guide/missing_data.rst b/doc/source/user_guide/missing_data.rst index 2e68a0598bb71..45e35b78eb390 100644 --- a/doc/source/user_guide/missing_data.rst +++ b/doc/source/user_guide/missing_data.rst @@ -190,7 +190,7 @@ The sum of an empty or all-NA Series or column of a DataFrame is 0. pd.Series([np.nan]).sum() - pd.Series([], dtype="float64").sum() + pd.Series([], dtype="float64", index=[]).sum() The product of an empty or all-NA Series or column of a DataFrame is 1. @@ -198,7 +198,7 @@ The product of an empty or all-NA Series or column of a DataFrame is 1. pd.Series([np.nan]).prod() - pd.Series([], dtype="float64").prod() + pd.Series([], dtype="float64", index=[]).prod() NA values in GroupBy diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 07849702c646d..3d733bb3352f2 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -406,6 +406,7 @@ Deprecations arguments (:issue:`27573`). - :func:`pandas.api.types.is_categorical` is deprecated and will be removed in a future version; use `:func:pandas.api.types.is_categorical_dtype` instead (:issue:`33385`) +- ``Series([])`` will raise a `DeprecationWarning` regarding its index. The default index type will change from :class:`RangeIndex` to :class:`Index` in a future version, matching the behaviour of ``Series()`` (:issue:`16737`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index e6967630b97ac..f551953fff862 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -53,7 +53,11 @@ ) from pandas.core.dtypes.missing import isna, na_value_for_dtype -from pandas.core.construction import array, extract_array +from pandas.core.construction import ( + array, + create_series_with_explicit_index, + extract_array, +) from pandas.core.indexers import validate_indices if TYPE_CHECKING: @@ -835,7 +839,7 @@ def mode(values, dropna: bool = True) -> "Series": warn(f"Unable to sort modes: {err}") result = _reconstruct_data(result, original.dtype, original) - return Series(result) + return create_series_with_explicit_index(result) def rank( diff --git a/pandas/core/apply.py b/pandas/core/apply.py index a013434491589..ba27e11eea4f0 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -16,7 +16,10 @@ ) from pandas.core.dtypes.generic import ABCSeries -from pandas.core.construction import create_series_with_explicit_dtype +from pandas.core.construction import ( + create_series_with_explicit_dtype, + create_series_with_explicit_index, +) if TYPE_CHECKING: from pandas import DataFrame, Series, Index @@ -202,7 +205,7 @@ def apply_empty_result(self): if not should_reduce: try: - r = self.f(Series([], dtype=np.float64)) + r = self.f(create_series_with_explicit_index([], dtype=np.float64)) except Exception: pass else: @@ -210,7 +213,7 @@ def apply_empty_result(self): if should_reduce: if len(self.agg_axis): - r = self.f(Series([], dtype=np.float64)) + r = self.f(create_series_with_explicit_index([], dtype=np.float64)) else: r = np.nan diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 2d60ad9ba50bf..fbc02f9e76bc9 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -621,10 +621,63 @@ def create_series_with_explicit_dtype( ------- Series """ - from pandas.core.series import Series + from pandas import RangeIndex if is_empty_data(data) and dtype is None: dtype = dtype_if_empty + + return create_series_with_explicit_index( + data=data, + index=index, + dtype=dtype, + name=name, + copy=copy, + fastpath=fastpath, + index_if_empty=RangeIndex(0), # non-breaking yet + ) + + +def create_series_with_explicit_index( + data: Any = None, + index: Optional[Union[ArrayLike, "Index"]] = None, + dtype: Optional[Dtype] = None, + name: Optional[str] = None, + copy: bool = False, + fastpath: bool = False, + index_if_empty: Optional["Index"] = None, +) -> "Series": + """ + Helper to pass an explicit index type when instantiating an Series where + data is list-like and empty. + + This silences a DeprecationWarning described in GitHub-16737. + + Parameters + ---------- + data : Mirrored from Series.__init__ + index : Mirrored from Series.__init__ + dtype : Mirrored from Series.__init__ + name : Mirrored from Series.__init__ + copy : Mirrored from Series.__init__ + fastpath : Mirrored from Series.__init__ + index_if_empty : instance of (Index, RangeIndex) + This index type will be passed explicitly when Series is initialised + with `data` being list-like and empty. + + Returns + ------- + Series + """ + from pandas import Index, Series # noqa: F811 + + # to avoid circular imports + if index_if_empty is None: + index_if_empty = Index([]) + + # dict's are handled separately in Series.__init__ + is_relevant_type = is_list_like(data) and not isinstance(data, dict) + if index is None and is_relevant_type and len(data) == 0: + index = index_if_empty return Series( data=data, index=index, dtype=dtype, name=name, copy=copy, fastpath=fastpath ) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 5cad76fd18c58..7ef479fd7ccbc 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -71,6 +71,7 @@ from pandas.core.arrays.datetimes import tz_to_dtype, validate_tz_from_dtype from pandas.core.base import IndexOpsMixin, PandasObject import pandas.core.common as com +from pandas.core.construction import create_series_with_explicit_index from pandas.core.indexers import deprecate_ndim_indexing from pandas.core.indexes.frozen import FrozenList import pandas.core.missing as missing @@ -142,9 +143,7 @@ def index_arithmetic_method(self, other): if isinstance(other, (ABCSeries, ABCDataFrame, ABCTimedeltaIndex)): return NotImplemented - from pandas import Series - - result = op(Series(self), other) + result = op(create_series_with_explicit_index(self), other) if isinstance(result, tuple): return (Index(result[0]), Index(result[1])) return Index(result) diff --git a/pandas/core/series.py b/pandas/core/series.py index 9ef865a964123..1dbd2eba8179e 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -308,7 +308,18 @@ def __init__( if index is None: if not is_list_like(data): data = [data] - index = ibase.default_index(len(data)) + + n = len(data) + if n == 0: + # gh-16737 + warnings.warn( + "The default index type for empty data will be 'Index' " + "instead of 'RangeIndex' in a future version. " + "Specify an index explicitly to silence this warning.", + DeprecationWarning, + stacklevel=2, + ) + index = ibase.default_index(n) elif is_list_like(data): # a scalar numpy array is list-like but doesn't diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 76b851d8ac923..1f0681e0f1190 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -36,7 +36,7 @@ from pandas.core.algorithms import take_1d from pandas.core.base import NoNewAttributesMixin -from pandas.core.construction import extract_array +from pandas.core.construction import create_series_with_explicit_index, extract_array if TYPE_CHECKING: from pandas.arrays import StringArray @@ -2180,7 +2180,7 @@ def _wrap_result( returns_string=True, ): - from pandas import Index, Series, MultiIndex + from pandas import Index, MultiIndex # for category, we do the stuff on the categories, so blow it up # to the full series again @@ -2190,7 +2190,9 @@ def _wrap_result( if use_codes and self._is_categorical: # if self._orig is a CategoricalIndex, there is no .cat-accessor result = take_1d( - result, Series(self._orig, copy=False).cat.codes, fill_value=fill_value + result, + create_series_with_explicit_index(self._orig, copy=False).cat.codes, + fill_value=fill_value, ) if not hasattr(result, "ndim") or not hasattr(result, "dtype"): diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index 207c5cc98449a..25e5de2d1b1c6 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -41,6 +41,7 @@ from pandas.core import algorithms from pandas.core.algorithms import unique from pandas.core.arrays.datetimes import tz_to_dtype +from pandas.core.construction import create_series_with_explicit_index # --------------------------------------------------------------------- # types used in annotations @@ -764,9 +765,10 @@ def to_datetime( if errors == "raise": raise # ... otherwise, continue without the cache. - from pandas import Series - cache_array = Series([], dtype=object) # just an empty array + cache_array = create_series_with_explicit_index( + [], dtype=object + ) # just an empty array if not cache_array.empty: result = _convert_and_box_cache(arg, cache_array) else: diff --git a/pandas/io/parsers.py b/pandas/io/parsers.py index 2df81ba0aa51a..f685b01179282 100644 --- a/pandas/io/parsers.py +++ b/pandas/io/parsers.py @@ -53,6 +53,7 @@ from pandas.core import algorithms from pandas.core.arrays import Categorical +from pandas.core.construction import create_series_with_explicit_index from pandas.core.frame import DataFrame from pandas.core.indexes.api import ( Index, @@ -60,7 +61,6 @@ RangeIndex, ensure_index_from_sequences, ) -from pandas.core.series import Series from pandas.core.tools import datetimes as tools from pandas.io.common import ( @@ -3494,14 +3494,20 @@ def _get_empty_meta(columns, index_col, index_names, dtype=None): if (index_col is None or index_col is False) or index_names is None: index = Index([]) else: - data = [Series([], dtype=dtype[name]) for name in index_names] + data = [ + create_series_with_explicit_index([], dtype=dtype[name]) + for name in index_names + ] index = ensure_index_from_sequences(data, names=index_names) index_col.sort() for i, n in enumerate(index_col): columns.pop(n - i) - col_dict = {col_name: Series([], dtype=dtype[col_name]) for col_name in columns} + col_dict = { + col_name: create_series_with_explicit_index([], dtype=dtype[col_name]) + for col_name in columns + } return index, columns, col_dict diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 311d8d0d55341..7a16cc8d32e3e 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -51,6 +51,7 @@ from pandas.core.arrays import Categorical, DatetimeArray, PeriodArray import pandas.core.common as com from pandas.core.computation.pytables import PyTablesExpr, maybe_expression +from pandas.core.construction import create_series_with_explicit_index from pandas.core.indexes.api import ensure_index from pandas.io.common import stringify_path @@ -3313,7 +3314,7 @@ def write_metadata(self, key: str, values: np.ndarray): key : str values : ndarray """ - values = Series(values) + values = create_series_with_explicit_index(values) self.parent.put( self._get_metadata_path(key), values, @@ -4051,7 +4052,9 @@ def read_column( encoding=self.encoding, errors=self.errors, ) - return Series(_set_tz(col_values[1], a.tz), name=column) + return create_series_with_explicit_index( + _set_tz(col_values[1], a.tz), name=column + ) raise KeyError(f"column [{column}] not found in the table") diff --git a/pandas/tests/arrays/boolean/test_reduction.py b/pandas/tests/arrays/boolean/test_reduction.py index a5c18a25f8e16..2c4cbaa0d9536 100644 --- a/pandas/tests/arrays/boolean/test_reduction.py +++ b/pandas/tests/arrays/boolean/test_reduction.py @@ -2,6 +2,7 @@ import pytest import pandas as pd +from pandas.core.construction import create_series_with_explicit_index @pytest.fixture @@ -31,7 +32,7 @@ def test_any_all(values, exp_any, exp_all, exp_any_noskip, exp_all_noskip): exp_any_noskip = pd.NA if exp_any_noskip is pd.NA else np.bool_(exp_any_noskip) exp_all_noskip = pd.NA if exp_all_noskip is pd.NA else np.bool_(exp_all_noskip) - for con in [pd.array, pd.Series]: + for con in [pd.array, create_series_with_explicit_index]: a = con(values, dtype="boolean") assert a.any() is exp_any assert a.all() is exp_all diff --git a/pandas/tests/arrays/integer/test_function.py b/pandas/tests/arrays/integer/test_function.py index bdf902d1aca62..937684465e47c 100644 --- a/pandas/tests/arrays/integer/test_function.py +++ b/pandas/tests/arrays/integer/test_function.py @@ -4,6 +4,7 @@ import pandas as pd import pandas._testing as tm from pandas.core.arrays import integer_array +from pandas.core.construction import create_series_with_explicit_index @pytest.mark.parametrize("ufunc", [np.abs, np.sign]) @@ -105,7 +106,7 @@ def test_value_counts_na(): def test_value_counts_empty(): # https://github.com/pandas-dev/pandas/issues/33317 - s = pd.Series([], dtype="Int64") + s = create_series_with_explicit_index([], dtype="Int64") result = s.value_counts() # TODO: The dtype of the index seems wrong (it's int64 for non-empty) idx = pd.Index([], dtype="object") diff --git a/pandas/tests/base/test_unique.py b/pandas/tests/base/test_unique.py index c6225c9b5ca64..7df2f8e8df7fe 100644 --- a/pandas/tests/base/test_unique.py +++ b/pandas/tests/base/test_unique.py @@ -7,6 +7,7 @@ import pandas as pd import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index from pandas.tests.base.common import allow_na_ops @@ -94,7 +95,9 @@ def test_nunique_null(null_obj, index_or_series_obj): else: values[0:2] = null_obj - klass = type(obj) + klass = ( + create_series_with_explicit_index if isinstance(obj, pd.Series) else type(obj) + ) repeated_values = np.repeat(values, range(1, len(values) + 1)) obj = klass(repeated_values, dtype=obj.dtype) diff --git a/pandas/tests/base/test_value_counts.py b/pandas/tests/base/test_value_counts.py index d45feaff68dde..5ee30d367dfa8 100644 --- a/pandas/tests/base/test_value_counts.py +++ b/pandas/tests/base/test_value_counts.py @@ -21,6 +21,7 @@ TimedeltaIndex, ) import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index from pandas.tests.base.common import allow_na_ops @@ -180,7 +181,7 @@ def test_value_counts_bins(index_or_series): assert s.nunique() == 3 s = klass({}) if klass is dict else klass({}, dtype=object) - expected = Series([], dtype=np.int64) + expected = create_series_with_explicit_index([], dtype=np.int64) tm.assert_series_equal(s.value_counts(), expected, check_index_type=False) # returned dtype differs depending on original if isinstance(s, Index): diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py index b9c8f3a8dd494..5d8215afb7f7e 100644 --- a/pandas/tests/dtypes/test_common.py +++ b/pandas/tests/dtypes/test_common.py @@ -20,6 +20,7 @@ import pandas as pd import pandas._testing as tm from pandas.arrays import SparseArray +from pandas.core.construction import create_series_with_explicit_index # EA & Actual Dtypes @@ -236,7 +237,9 @@ def test_is_timedelta64_dtype(): assert not com.is_timedelta64_dtype("NO DATE") assert com.is_timedelta64_dtype(np.timedelta64) - assert com.is_timedelta64_dtype(pd.Series([], dtype="timedelta64[ns]")) + assert com.is_timedelta64_dtype( + create_series_with_explicit_index([], dtype="timedelta64[ns]") + ) assert com.is_timedelta64_dtype(pd.to_timedelta(["0 days", "1 days"])) @@ -499,7 +502,9 @@ def test_needs_i8_conversion(): assert not com.needs_i8_conversion(np.array(["a", "b"])) assert com.needs_i8_conversion(np.datetime64) - assert com.needs_i8_conversion(pd.Series([], dtype="timedelta64[ns]")) + assert com.needs_i8_conversion( + create_series_with_explicit_index([], dtype="timedelta64[ns]") + ) assert com.needs_i8_conversion(pd.DatetimeIndex(["2000"], tz="US/Eastern")) @@ -567,7 +572,7 @@ def test_is_extension_type(check_scipy): assert com.is_extension_type(pd.DatetimeIndex(["2000"], tz="US/Eastern")) dtype = DatetimeTZDtype("ns", tz="US/Eastern") - s = pd.Series([], dtype=dtype) + s = create_series_with_explicit_index([], dtype=dtype) assert com.is_extension_type(s) if check_scipy: @@ -596,7 +601,7 @@ def test_is_extension_array_dtype(check_scipy): assert com.is_extension_array_dtype(pd.DatetimeIndex(["2000"], tz="US/Eastern")) dtype = DatetimeTZDtype("ns", tz="US/Eastern") - s = pd.Series([], dtype=dtype) + s = create_series_with_explicit_index([], dtype=dtype) assert com.is_extension_array_dtype(s) if check_scipy: diff --git a/pandas/tests/dtypes/test_concat.py b/pandas/tests/dtypes/test_concat.py index 1fbbd3356ae13..584d1b3bb6d59 100644 --- a/pandas/tests/dtypes/test_concat.py +++ b/pandas/tests/dtypes/test_concat.py @@ -5,6 +5,7 @@ import pandas as pd from pandas import DatetimeIndex, Period, PeriodIndex, Series, TimedeltaIndex import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index @pytest.mark.parametrize( @@ -83,7 +84,7 @@ def test_get_dtype_kinds_period(to_concat, expected): def test_concat_mismatched_categoricals_with_empty(): # concat_compat behavior on series._values should match pd.concat on series ser1 = Series(["a", "b", "c"], dtype="category") - ser2 = Series([], dtype="category") + ser2 = create_series_with_explicit_index([], dtype="category") result = _concat.concat_compat([ser1._values, ser2._values]) expected = pd.concat([ser1, ser2])._values diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index 8c0580b7cf047..505aeda4acbcf 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -52,6 +52,10 @@ ) import pandas._testing as tm from pandas.core.arrays import IntegerArray +from pandas.core.construction import ( + create_series_with_explicit_dtype, + create_series_with_explicit_index, +) @pytest.fixture(params=[True, False], ids=str) @@ -77,9 +81,9 @@ def coerce(request): ((x for x in [1, 2]), True, "generator"), ((_ for _ in []), True, "generator-empty"), (Series([1]), True, "Series"), - (Series([], dtype=object), True, "Series-empty"), + (create_series_with_explicit_dtype([], dtype=object), True, "Series-empty"), (Series(["a"]).str, True, "StringMethods"), - (Series([], dtype="O").str, True, "StringMethods-empty"), + (create_series_with_explicit_dtype([], dtype="O").str, True, "StringMethods-empty"), (Index([1]), True, "Index"), (Index([]), True, "Index-empty"), (DataFrame([[1]]), True, "DataFrame"), @@ -138,7 +142,7 @@ def __getitem__(self): def test_is_array_like(): - assert inference.is_array_like(Series([], dtype=object)) + assert inference.is_array_like(create_series_with_explicit_index([], dtype=object)) assert inference.is_array_like(Series([1, 2])) assert inference.is_array_like(np.array(["a", "b"])) assert inference.is_array_like(Index(["2016-01-01"])) @@ -164,7 +168,7 @@ class DtypeList(list): {"a": 1}, {1, "a"}, Series([1]), - Series([], dtype=object), + create_series_with_explicit_index([], dtype=object), Series(["a"]).str, (x for x in range(5)), ], diff --git a/pandas/tests/extension/base/missing.py b/pandas/tests/extension/base/missing.py index 2393d2edcd2c6..1049edac328e7 100644 --- a/pandas/tests/extension/base/missing.py +++ b/pandas/tests/extension/base/missing.py @@ -19,7 +19,7 @@ def test_isna(self, data_missing): # GH 21189 result = pd.Series(data_missing).drop([0, 1]).isna() - expected = pd.Series([], dtype=bool) + expected = pd.Series([], dtype=bool, index=pd.RangeIndex(0)) self.assert_series_equal(result, expected) def test_dropna_array(self, data_missing): diff --git a/pandas/tests/extension/test_common.py b/pandas/tests/extension/test_common.py index e43650c291200..bdb49d2f86a0c 100644 --- a/pandas/tests/extension/test_common.py +++ b/pandas/tests/extension/test_common.py @@ -7,6 +7,7 @@ import pandas as pd import pandas._testing as tm from pandas.core.arrays import ExtensionArray +from pandas.core.construction import create_series_with_explicit_index class DummyDtype(dtypes.ExtensionDtype): @@ -40,7 +41,7 @@ class TestExtensionArrayDtype: [ pd.Categorical([]), pd.Categorical([]).dtype, - pd.Series(pd.Categorical([])), + create_series_with_explicit_index(pd.Categorical([])), DummyDtype(), DummyArray(np.array([1, 2])), ], @@ -48,7 +49,9 @@ class TestExtensionArrayDtype: def test_is_extension_array_dtype(self, values): assert is_extension_array_dtype(values) - @pytest.mark.parametrize("values", [np.array([]), pd.Series(np.array([]))]) + @pytest.mark.parametrize( + "values", [np.array([]), create_series_with_explicit_index(np.array([]))] + ) def test_is_not_extension_array_dtype(self, values): assert not is_extension_array_dtype(values) diff --git a/pandas/tests/extension/test_sparse.py b/pandas/tests/extension/test_sparse.py index 694bbee59606f..a71dec28b1e29 100644 --- a/pandas/tests/extension/test_sparse.py +++ b/pandas/tests/extension/test_sparse.py @@ -187,7 +187,7 @@ def test_isna(self, data_missing): # GH 21189 result = pd.Series(data_missing).drop([0, 1]).isna() - expected = pd.Series([], dtype=expected_dtype) + expected = pd.Series([], dtype=expected_dtype, index=pd.RangeIndex(0)) self.assert_series_equal(result, expected) def test_fillna_limit_pad(self, data_missing): diff --git a/pandas/tests/frame/methods/test_replace.py b/pandas/tests/frame/methods/test_replace.py index a9fb686d5bc50..140b7aafb64e4 100644 --- a/pandas/tests/frame/methods/test_replace.py +++ b/pandas/tests/frame/methods/test_replace.py @@ -9,6 +9,7 @@ import pandas as pd from pandas import DataFrame, Index, Series, Timestamp, date_range import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index @pytest.fixture @@ -1251,7 +1252,9 @@ def test_replace_with_empty_dictlike(self, mix_abc): # GH 15289 df = DataFrame(mix_abc) tm.assert_frame_equal(df, df.replace({})) - tm.assert_frame_equal(df, df.replace(Series([], dtype=object))) + tm.assert_frame_equal( + df, df.replace(create_series_with_explicit_index([], dtype=object)) + ) tm.assert_frame_equal(df, df.replace({"b": {}})) tm.assert_frame_equal(df, df.replace(Series({"b": {}}))) diff --git a/pandas/tests/frame/methods/test_value_counts.py b/pandas/tests/frame/methods/test_value_counts.py index c409b0bbe6fa9..4b2e8148c2ddb 100644 --- a/pandas/tests/frame/methods/test_value_counts.py +++ b/pandas/tests/frame/methods/test_value_counts.py @@ -88,7 +88,7 @@ def test_data_frame_value_counts_empty(): df_no_cols = pd.DataFrame() result = df_no_cols.value_counts() - expected = pd.Series([], dtype=np.int64) + expected = pd.Series([], dtype=np.int64, index=pd.RangeIndex(0)) tm.assert_series_equal(result, expected) @@ -97,6 +97,6 @@ def test_data_frame_value_counts_empty_normalize(): df_no_cols = pd.DataFrame() result = df_no_cols.value_counts(normalize=True) - expected = pd.Series([], dtype=np.float64) + expected = pd.Series([], dtype=np.float64, index=pd.RangeIndex(0)) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/frame/test_arithmetic.py b/pandas/tests/frame/test_arithmetic.py index d929d3e030508..a93796d9d974b 100644 --- a/pandas/tests/frame/test_arithmetic.py +++ b/pandas/tests/frame/test_arithmetic.py @@ -11,6 +11,7 @@ from pandas import DataFrame, MultiIndex, Series import pandas._testing as tm import pandas.core.common as com +from pandas.core.construction import create_series_with_explicit_index from pandas.tests.frame.common import _check_mixed_float, _check_mixed_int # ------------------------------------------------------------------- @@ -522,7 +523,7 @@ def test_arith_flex_series(self, simple_frame): def test_arith_flex_zero_len_raises(self): # GH 19522 passing fill_value to frame flex arith methods should # raise even in the zero-length special cases - ser_len0 = pd.Series([], dtype=object) + ser_len0 = create_series_with_explicit_index([], dtype=object) df_len0 = pd.DataFrame(columns=["A", "B"]) df = pd.DataFrame([[1, 2], [3, 4]], columns=["A", "B"]) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index baac87755c6d2..fcfd32500d875 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -30,7 +30,10 @@ ) import pandas._testing as tm from pandas.arrays import IntervalArray, PeriodArray, SparseArray -from pandas.core.construction import create_series_with_explicit_dtype +from pandas.core.construction import ( + create_series_with_explicit_dtype, + create_series_with_explicit_index, +) MIXED_FLOAT_DTYPES = ["float16", "float32", "float64"] MIXED_INT_DTYPES = [ @@ -1542,7 +1545,7 @@ def test_constructor_Series_named(self): DataFrame(s, columns=[1, 2]) # #2234 - a = Series([], name="x", dtype=object) + a = create_series_with_explicit_index([], name="x", dtype=object) df = DataFrame(a) assert df.columns[0] == "x" diff --git a/pandas/tests/generic/test_generic.py b/pandas/tests/generic/test_generic.py index 05588ead54be4..1b7cd347f7b31 100644 --- a/pandas/tests/generic/test_generic.py +++ b/pandas/tests/generic/test_generic.py @@ -11,6 +11,7 @@ from pandas import DataFrame, MultiIndex, Series, date_range import pandas._testing as tm import pandas.core.common as com +from pandas.core.construction import create_series_with_explicit_index # ---------------------------------------------------------------------- # Generic types test cases @@ -54,7 +55,9 @@ def _construct(self, shape, value=None, dtype=None, **kwargs): arr = np.repeat(arr, new_shape).reshape(shape) else: arr = np.random.randn(*shape) - return self._typ(arr, dtype=dtype, **kwargs) + + typ = create_series_with_explicit_index if self._typ is Series else self._typ + return typ(arr, dtype=dtype, **kwargs) def _compare(self, result, expected): self._comparator(result, expected) @@ -680,7 +683,9 @@ def test_squeeze(self): tm.assert_series_equal(df.squeeze(), df["A"]) # don't fail with 0 length dimensions GH11229 & GH8999 - empty_series = Series([], name="five", dtype=np.float64) + empty_series = create_series_with_explicit_index( + [], name="five", dtype=np.float64 + ) empty_frame = DataFrame([empty_series]) tm.assert_series_equal(empty_series, empty_series.squeeze()) tm.assert_series_equal(empty_series, empty_frame.squeeze()) diff --git a/pandas/tests/generic/test_to_xarray.py b/pandas/tests/generic/test_to_xarray.py index 2fde96a1c8f89..19b2ef6343811 100644 --- a/pandas/tests/generic/test_to_xarray.py +++ b/pandas/tests/generic/test_to_xarray.py @@ -6,6 +6,7 @@ import pandas as pd from pandas import DataFrame, Series import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index class TestDataFrameToXArray: @@ -115,7 +116,7 @@ def test_to_xarray_index_types(self, indices): def test_to_xarray(self): from xarray import DataArray - s = Series([], dtype=object) + s = create_series_with_explicit_index([], dtype=object) s.index.name = "foo" result = s.to_xarray() assert len(result) == 0 diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index 8e4a7141875bb..de96056b5bd93 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -16,6 +16,7 @@ qcut, ) import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index def cartesian_product_for_groupers(result, args, names): @@ -792,7 +793,7 @@ def test_groupby_empty_with_category(): result = df.groupby("A").first()["B"] expected = pd.Series( pd.Categorical([], categories=["test", "train"]), - index=pd.Series([], dtype="object", name="A"), + index=create_series_with_explicit_index([], dtype="object", name="A"), name="B", ) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index efcd22f9c0c82..895a21ccff18e 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -14,6 +14,7 @@ date_range, ) import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index from pandas.core.groupby.grouper import Grouping # selection @@ -637,11 +638,12 @@ def test_evaluate_with_empty_groups(self, func, expected): def test_groupby_empty(self): # https://github.com/pandas-dev/pandas/issues/27190 - s = pd.Series([], name="name", dtype="float64") + s = create_series_with_explicit_index([], name="name", dtype="float64") gr = s.groupby([]) result = gr.mean() - tm.assert_series_equal(result, s) + expected = pd.Series([], name="name", dtype="float64", index=pd.RangeIndex(0)) + tm.assert_series_equal(result, expected) # check group properties assert len(gr.grouper.groupings) == 1 diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index fd23e95106ab0..126339c260ea5 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -25,6 +25,7 @@ isna, ) import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index from pandas.core.indexes.base import InvalidIndexError from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin @@ -429,7 +430,10 @@ def test_intersection_base(self, indices): return # GH 10149 - cases = [klass(second.values) for klass in [np.array, Series, list]] + cases = [ + klass(second.values) + for klass in [np.array, create_series_with_explicit_index, list] + ] for case in cases: result = first.intersection(case) assert tm.equalContents(result, second) @@ -452,7 +456,10 @@ def test_union_base(self, indices): return # GH 10149 - cases = [klass(second.values) for klass in [np.array, Series, list]] + cases = [ + klass(second.values) + for klass in [np.array, create_series_with_explicit_index, list] + ] for case in cases: if not isinstance(indices, CategoricalIndex): result = first.union(case) @@ -474,7 +481,10 @@ def test_difference_base(self, sort, indices): assert tm.equalContents(result, answer) # GH 10149 - cases = [klass(second.values) for klass in [np.array, Series, list]] + cases = [ + klass(second.values) + for klass in [np.array, create_series_with_explicit_index, list] + ] for case in cases: if isinstance(indices, (DatetimeIndex, TimedeltaIndex)): assert type(result) == type(answer) @@ -564,7 +574,7 @@ def test_equals(self, indices): if indices.nlevels == 1: # do not test MultiIndex - assert not indices.equals(Series(indices)) + assert not indices.equals(create_series_with_explicit_index(indices)) def test_equals_op(self): # GH9947, GH10637 diff --git a/pandas/tests/indexes/datetimes/test_formats.py b/pandas/tests/indexes/datetimes/test_formats.py index f34019e06fd5f..424c80a409ffd 100644 --- a/pandas/tests/indexes/datetimes/test_formats.py +++ b/pandas/tests/indexes/datetimes/test_formats.py @@ -6,8 +6,9 @@ import pytz import pandas as pd -from pandas import DatetimeIndex, Series +from pandas import DatetimeIndex import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index def test_to_native_types(): @@ -168,7 +169,7 @@ def test_dti_representation_to_series(self): [idx1, idx2, idx3, idx4, idx5, idx6, idx7], [exp1, exp2, exp3, exp4, exp5, exp6, exp7], ): - result = repr(Series(idx)) + result = repr(create_series_with_explicit_index(idx)) assert result == expected def test_dti_summary(self): diff --git a/pandas/tests/indexes/period/test_formats.py b/pandas/tests/indexes/period/test_formats.py index 5db373a9f07ae..bdfdb6928449e 100644 --- a/pandas/tests/indexes/period/test_formats.py +++ b/pandas/tests/indexes/period/test_formats.py @@ -4,6 +4,7 @@ import pandas as pd from pandas import PeriodIndex import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index def test_to_native_types(): @@ -160,7 +161,7 @@ def test_representation_to_series(self): [idx1, idx2, idx3, idx4, idx5, idx6, idx7, idx8, idx9], [exp1, exp2, exp3, exp4, exp5, exp6, exp7, exp8, exp9], ): - result = repr(pd.Series(idx)) + result = repr(create_series_with_explicit_index(idx)) assert result == expected def test_summary(self): diff --git a/pandas/tests/indexes/period/test_period.py b/pandas/tests/indexes/period/test_period.py index 0ce10fb8779a1..0d9c0b1d899f3 100644 --- a/pandas/tests/indexes/period/test_period.py +++ b/pandas/tests/indexes/period/test_period.py @@ -18,6 +18,7 @@ period_range, ) import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index from ..datetimelike import DatetimeLike @@ -262,7 +263,7 @@ def _check_all_fields(self, periodindex): ] periods = list(periodindex) - s = pd.Series(periodindex) + s = create_series_with_explicit_index(periodindex) for field in fields: field_idx = getattr(periodindex, field) diff --git a/pandas/tests/indexes/timedeltas/test_formats.py b/pandas/tests/indexes/timedeltas/test_formats.py index 1dfc5b5305008..6076fa67361ed 100644 --- a/pandas/tests/indexes/timedeltas/test_formats.py +++ b/pandas/tests/indexes/timedeltas/test_formats.py @@ -2,6 +2,7 @@ import pandas as pd from pandas import TimedeltaIndex +from pandas.core.construction import create_series_with_explicit_index class TestTimedeltaIndexRendering: @@ -62,7 +63,7 @@ def test_representation_to_series(self): for idx, expected in zip( [idx1, idx2, idx3, idx4, idx5], [exp1, exp2, exp3, exp4, exp5] ): - result = repr(pd.Series(idx)) + result = repr(create_series_with_explicit_index(idx)) assert result == expected def test_summary(self): diff --git a/pandas/tests/indexing/multiindex/test_loc.py b/pandas/tests/indexing/multiindex/test_loc.py index b7802d9b8fe0c..cd59b3b087299 100644 --- a/pandas/tests/indexing/multiindex/test_loc.py +++ b/pandas/tests/indexing/multiindex/test_loc.py @@ -4,6 +4,7 @@ import pandas as pd from pandas import DataFrame, Index, MultiIndex, Series import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index from pandas.core.indexing import IndexingError @@ -46,7 +47,7 @@ def test_loc_getitem_series(self): result = x.loc[y1] tm.assert_series_equal(result, expected) - empty = Series(data=[], dtype=np.float64) + empty = create_series_with_explicit_index(data=[], dtype=np.float64) expected = Series( [], index=MultiIndex(levels=index.levels, codes=[[], []], dtype=np.float64), diff --git a/pandas/tests/indexing/test_partial.py b/pandas/tests/indexing/test_partial.py index 2e691c6fd76d8..9d6ab4314c867 100644 --- a/pandas/tests/indexing/test_partial.py +++ b/pandas/tests/indexing/test_partial.py @@ -10,6 +10,7 @@ import pandas as pd from pandas import DataFrame, Index, Series, date_range import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index class TestPartialSetting: @@ -401,14 +402,14 @@ def test_partial_set_empty_frame(self): def f(): df = DataFrame(index=Index([], dtype="object")) - df["foo"] = Series([], dtype="object") + df["foo"] = create_series_with_explicit_index([], dtype="object") return df tm.assert_frame_equal(f(), expected) def f(): df = DataFrame() - df["foo"] = Series(df.index) + df["foo"] = create_series_with_explicit_index(df.index) return df tm.assert_frame_equal(f(), expected) @@ -432,7 +433,9 @@ def f(): def f(): df = DataFrame(index=Index([], dtype="int64")) - df["foo"] = Series(np.arange(len(df)), dtype="float64") + df["foo"] = create_series_with_explicit_index( + np.arange(len(df)), dtype="float64" + ) return df tm.assert_frame_equal(f(), expected) diff --git a/pandas/tests/io/parser/test_dtypes.py b/pandas/tests/io/parser/test_dtypes.py index e68dcb3aa577e..12f03c3704c6c 100644 --- a/pandas/tests/io/parser/test_dtypes.py +++ b/pandas/tests/io/parser/test_dtypes.py @@ -13,8 +13,9 @@ from pandas.core.dtypes.dtypes import CategoricalDtype import pandas as pd -from pandas import Categorical, DataFrame, Index, MultiIndex, Series, Timestamp, concat +from pandas import Categorical, DataFrame, Index, MultiIndex, Timestamp, concat import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index @pytest.mark.parametrize("dtype", [str, object]) @@ -432,7 +433,10 @@ def test_empty_with_dup_column_pass_dtype_by_indexes(all_parsers): # see gh-9424 parser = all_parsers expected = concat( - [Series([], name="one", dtype="u1"), Series([], name="one.1", dtype="f")], + [ + create_series_with_explicit_index([], name="one", dtype="u1"), + create_series_with_explicit_index([], name="one.1", dtype="f"), + ], axis=1, ) expected.index = expected.index.astype(object) @@ -446,7 +450,10 @@ def test_empty_with_dup_column_pass_dtype_by_indexes_raises(all_parsers): # see gh-9424 parser = all_parsers expected = concat( - [Series([], name="one", dtype="u1"), Series([], name="one.1", dtype="f")], + [ + create_series_with_explicit_index([], name="one", dtype="u1"), + create_series_with_explicit_index([], name="one.1", dtype="f"), + ], axis=1, ) expected.index = expected.index.astype(object) @@ -502,8 +509,8 @@ def test_dtype_with_converters(all_parsers): "timedelta64[ns]", DataFrame( { - "a": Series([], dtype="timedelta64[ns]"), - "b": Series([], dtype="timedelta64[ns]"), + "a": create_series_with_explicit_index([], dtype="timedelta64[ns]"), + "b": create_series_with_explicit_index([], dtype="timedelta64[ns]"), }, index=[], ), @@ -511,21 +518,30 @@ def test_dtype_with_converters(all_parsers): ( dict(a=np.int64, b=np.int32), DataFrame( - {"a": Series([], dtype=np.int64), "b": Series([], dtype=np.int32)}, + { + "a": create_series_with_explicit_index([], dtype=np.int64), + "b": create_series_with_explicit_index([], dtype=np.int32), + }, index=[], ), ), ( {0: np.int64, 1: np.int32}, DataFrame( - {"a": Series([], dtype=np.int64), "b": Series([], dtype=np.int32)}, + { + "a": create_series_with_explicit_index([], dtype=np.int64), + "b": create_series_with_explicit_index([], dtype=np.int32), + }, index=[], ), ), ( {"a": np.int64, 1: np.int32}, DataFrame( - {"a": Series([], dtype=np.int64), "b": Series([], dtype=np.int32)}, + { + "a": create_series_with_explicit_index([], dtype=np.int64), + "b": create_series_with_explicit_index([], dtype=np.int32), + }, index=[], ), ), diff --git a/pandas/tests/io/parser/test_unsupported.py b/pandas/tests/io/parser/test_unsupported.py index 267fae760398a..ba0b318bfe524 100644 --- a/pandas/tests/io/parser/test_unsupported.py +++ b/pandas/tests/io/parser/test_unsupported.py @@ -55,7 +55,10 @@ def test_c_engine(self): read_csv(StringIO(data), sep=r"\s") with tm.assert_produces_warning(parsers.ParserWarning): read_csv(StringIO(data), sep="\t", quotechar=chr(128)) - with tm.assert_produces_warning(parsers.ParserWarning): + with tm.assert_produces_warning( + parsers.ParserWarning, raise_on_extra_warnings=False + ): + # Additionally raises DeprecationWarning: gh-16737 read_csv(StringIO(data), skipfooter=1) text = """ A B C D E diff --git a/pandas/tests/reductions/test_reductions.py b/pandas/tests/reductions/test_reductions.py index 235aa8e4aa922..ca26035bb87a8 100644 --- a/pandas/tests/reductions/test_reductions.py +++ b/pandas/tests/reductions/test_reductions.py @@ -23,6 +23,7 @@ ) import pandas._testing as tm from pandas.core import nanops +from pandas.core.construction import create_series_with_explicit_index def get_objs(): @@ -78,6 +79,7 @@ def test_ops(self, opname, obj): def test_nanminmax(self, opname, dtype, val, index_or_series): # GH#7261 klass = index_or_series + klass = create_series_with_explicit_index if klass is Series else klass if dtype in ["Int64", "boolean"] and klass == pd.Index: pytest.skip("EAs can't yet be stored in an index") @@ -137,6 +139,7 @@ def test_nanargminmax(self, opname, index_or_series): @pytest.mark.parametrize("dtype", ["M8[ns]", "datetime64[ns, UTC]"]) def test_nanops_empty_object(self, opname, index_or_series, dtype): klass = index_or_series + klass = create_series_with_explicit_index if klass is Series else klass arg_op = "arg" + opname if klass is Index else "idx" + opname obj = klass([], dtype=dtype) @@ -566,7 +569,7 @@ def test_empty(self, method, unit, use_bottleneck, dtype): with pd.option_context("use_bottleneck", use_bottleneck): # GH#9422 / GH#18921 # Entirely empty - s = Series([], dtype=dtype) + s = create_series_with_explicit_index([], dtype=dtype) # NA by default result = getattr(s, method)() assert result == unit @@ -690,7 +693,7 @@ def test_ops_consistency_on_empty(self, method): assert pd.isna(result) # timedelta64[ns] - tdser = Series([], dtype="m8[ns]") + tdser = create_series_with_explicit_index([], dtype="m8[ns]") if method == "var": msg = "|".join( [ @@ -740,10 +743,16 @@ def test_sum_overflow(self, use_bottleneck): def test_empty_timeseries_reductions_return_nat(self): # covers GH#11245 for dtype in ("m8[ns]", "m8[ns]", "M8[ns]", "M8[ns, UTC]"): - assert Series([], dtype=dtype).min() is pd.NaT - assert Series([], dtype=dtype).max() is pd.NaT - assert Series([], dtype=dtype).min(skipna=False) is pd.NaT - assert Series([], dtype=dtype).max(skipna=False) is pd.NaT + assert create_series_with_explicit_index([], dtype=dtype).min() is pd.NaT + assert create_series_with_explicit_index([], dtype=dtype).max() is pd.NaT + assert ( + create_series_with_explicit_index([], dtype=dtype).min(skipna=False) + is pd.NaT + ) + assert ( + create_series_with_explicit_index([], dtype=dtype).max(skipna=False) + is pd.NaT + ) def test_numpy_argmin(self): # See GH#16830 @@ -962,7 +971,7 @@ def test_timedelta64_analytics(self): @pytest.mark.parametrize( "test_input,error_type", [ - (pd.Series([], dtype="float64"), ValueError), + (create_series_with_explicit_index([], dtype="float64"), ValueError), # For strings, or any Series with dtype 'O' (pd.Series(["foo", "bar", "baz"]), TypeError), (pd.Series([(1,), (2,)]), TypeError), @@ -1128,10 +1137,13 @@ class TestSeriesMode: @pytest.mark.parametrize( "dropna, expected", - [(True, Series([], dtype=np.float64)), (False, Series([], dtype=np.float64))], + [ + (True, create_series_with_explicit_index([], dtype=np.float64)), + (False, create_series_with_explicit_index([], dtype=np.float64)), + ], ) def test_mode_empty(self, dropna, expected): - s = Series([], dtype=np.float64) + s = create_series_with_explicit_index([], dtype=np.float64) result = s.mode(dropna) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/resample/test_base.py b/pandas/tests/resample/test_base.py index 6384c5f19c898..94025cf16043c 100644 --- a/pandas/tests/resample/test_base.py +++ b/pandas/tests/resample/test_base.py @@ -6,6 +6,7 @@ import pandas as pd from pandas import DataFrame, Series import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index from pandas.core.groupby.groupby import DataError from pandas.core.groupby.grouper import Grouper from pandas.core.indexes.datetimes import date_range @@ -136,7 +137,7 @@ def test_resample_empty_dataframe(empty_frame_dti, freq, resample_method): expected = df.copy() else: # GH14962 - expected = Series([], dtype=object) + expected = create_series_with_explicit_index([], dtype=object) expected.index = _asfreq_compat(df.index, freq) diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index bccae2c4c2772..cb41a1403444c 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -29,7 +29,10 @@ ) import pandas._testing as tm from pandas.core.arrays import SparseArray -from pandas.core.construction import create_series_with_explicit_dtype +from pandas.core.construction import ( + create_series_with_explicit_dtype, + create_series_with_explicit_index, +) from pandas.tests.extension.decimal import to_decimal @@ -724,7 +727,7 @@ def test_concat_categorical_coercion_nan(self): def test_concat_categorical_empty(self): # GH 13524 - s1 = pd.Series([], dtype="category") + s1 = create_series_with_explicit_index([], dtype="category") s2 = pd.Series([1, 2], dtype="category") tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), s2) @@ -733,22 +736,24 @@ def test_concat_categorical_empty(self): tm.assert_series_equal(pd.concat([s2, s1], ignore_index=True), s2) tm.assert_series_equal(s2.append(s1, ignore_index=True), s2) - s1 = pd.Series([], dtype="category") - s2 = pd.Series([], dtype="category") + s1 = create_series_with_explicit_index([], dtype="category") + s2 = create_series_with_explicit_index([], dtype="category") + expected = pd.Series([], dtype="category", index=pd.RangeIndex(0)) - tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), s2) - tm.assert_series_equal(s1.append(s2, ignore_index=True), s2) + tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), expected) + tm.assert_series_equal(s1.append(s2, ignore_index=True), expected) - s1 = pd.Series([], dtype="category") - s2 = pd.Series([], dtype="object") + s1 = create_series_with_explicit_index([], dtype="category") + s2 = create_series_with_explicit_index([], dtype="object") + expected = pd.Series([], dtype="object", index=pd.RangeIndex(0)) # different dtype => not-category - tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), s2) - tm.assert_series_equal(s1.append(s2, ignore_index=True), s2) - tm.assert_series_equal(pd.concat([s2, s1], ignore_index=True), s2) - tm.assert_series_equal(s2.append(s1, ignore_index=True), s2) + tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), expected) + tm.assert_series_equal(s1.append(s2, ignore_index=True), expected) + tm.assert_series_equal(pd.concat([s2, s1], ignore_index=True), expected) + tm.assert_series_equal(s2.append(s1, ignore_index=True), expected) - s1 = pd.Series([], dtype="category") + s1 = create_series_with_explicit_index([], dtype="category") s2 = pd.Series([np.nan, np.nan]) # empty Series is ignored @@ -2195,17 +2200,20 @@ def test_concat_empty_series(self): def test_concat_empty_series_timelike(self, tz, values): # GH 18447 - first = Series([], dtype="M8[ns]").dt.tz_localize(tz) + first = create_series_with_explicit_index([], dtype="M8[ns]").dt.tz_localize(tz) dtype = None if values else np.float64 - second = Series(values, dtype=dtype) + second = create_series_with_explicit_index(values, dtype=dtype) + result = concat([first, second], axis=1) expected = DataFrame( { - 0: pd.Series([pd.NaT] * len(values), dtype="M8[ns]").dt.tz_localize(tz), + 0: create_series_with_explicit_index( + [pd.NaT] * len(values), dtype="M8[ns]" + ).dt.tz_localize(tz), 1: values, - } + }, ) - result = concat([first, second], axis=1) + expected.index = expected.index.astype(object) tm.assert_frame_equal(result, expected) def test_default_index(self): @@ -2595,7 +2603,7 @@ def test_concat_empty_and_non_empty_frame_regression(): def test_concat_empty_and_non_empty_series_regression(): # GH 18187 regression test s1 = pd.Series([1]) - s2 = pd.Series([], dtype=object) + s2 = create_series_with_explicit_index([], dtype=object) expected = s1 result = pd.concat([s1, s2]) diff --git a/pandas/tests/series/indexing/test_boolean.py b/pandas/tests/series/indexing/test_boolean.py index e2b71b1f2f412..6d7196535d230 100644 --- a/pandas/tests/series/indexing/test_boolean.py +++ b/pandas/tests/series/indexing/test_boolean.py @@ -3,6 +3,7 @@ from pandas import Index, Series import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index from pandas.core.indexing import IndexingError from pandas.tseries.offsets import BDay @@ -20,7 +21,7 @@ def test_getitem_boolean(string_series): def test_getitem_boolean_empty(): - s = Series([], dtype=np.int64) + s = create_series_with_explicit_index([], dtype=np.int64) s.index.name = "index_name" s = s[s.isna()] assert s.index.name == "index_name" @@ -30,7 +31,7 @@ def test_getitem_boolean_empty(): # indexing with empty series s = Series(["A", "B"]) expected = Series(dtype=object, index=Index([], dtype="int64")) - result = s[Series([], dtype=object)] + result = s[create_series_with_explicit_index([], dtype=object)] tm.assert_series_equal(result, expected) # invalid because of the boolean indexer @@ -40,7 +41,7 @@ def test_getitem_boolean_empty(): r"the boolean Series and of the indexed object do not match" ) with pytest.raises(IndexingError, match=msg): - s[Series([], dtype=bool)] + s[create_series_with_explicit_index([], dtype=bool)] with pytest.raises(IndexingError, match=msg): s[Series([True], dtype=bool)] diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index c2b5117d395f9..bc88e783cc232 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -10,6 +10,7 @@ import pandas as pd from pandas import Categorical, DataFrame, MultiIndex, Series, Timedelta, Timestamp import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index from pandas.tseries.offsets import BDay @@ -160,7 +161,7 @@ def test_getitem_out_of_bounds(datetime_series): # GH #917 # With a RangeIndex, an int key gives a KeyError - s = Series([], dtype=object) + s = Series([], index=pd.RangeIndex(0), dtype=object) with pytest.raises(KeyError, match="-1"): s[-1] @@ -617,7 +618,7 @@ def test_setitem_na(): def test_timedelta_assignment(): # GH 8209 - s = Series([], dtype=object) + s = create_series_with_explicit_index([], dtype=object) s.loc["B"] = timedelta(1) tm.assert_series_equal(s, Series(Timedelta("1 days"), index=["B"])) diff --git a/pandas/tests/series/methods/test_drop_duplicates.py b/pandas/tests/series/methods/test_drop_duplicates.py index a4532ebb3d8c5..255f92d8cf449 100644 --- a/pandas/tests/series/methods/test_drop_duplicates.py +++ b/pandas/tests/series/methods/test_drop_duplicates.py @@ -3,6 +3,7 @@ from pandas import Categorical, Series import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index @pytest.mark.parametrize( @@ -46,8 +47,8 @@ def test_drop_duplicates_bool(keep, expected): @pytest.mark.parametrize("values", [[], list(range(5))]) def test_drop_duplicates_no_duplicates(any_numpy_dtype, keep, values): - tc = Series(values, dtype=np.dtype(any_numpy_dtype)) - expected = Series([False] * len(tc), dtype="bool") + tc = create_series_with_explicit_index(values, dtype=np.dtype(any_numpy_dtype)) + expected = create_series_with_explicit_index([False] * len(tc), dtype="bool") if tc.dtype == "bool": # 0 -> False and 1-> True diff --git a/pandas/tests/series/methods/test_interpolate.py b/pandas/tests/series/methods/test_interpolate.py index 6844225a81a8f..b924d15c6bf16 100644 --- a/pandas/tests/series/methods/test_interpolate.py +++ b/pandas/tests/series/methods/test_interpolate.py @@ -169,7 +169,7 @@ def test_interpolate_corners(self, kwargs): s = Series([np.nan, np.nan]) tm.assert_series_equal(s.interpolate(**kwargs), s) - s = Series([], dtype=object).interpolate() + s = Series([], dtype=object, index=pd.RangeIndex(0)).interpolate() tm.assert_series_equal(s.interpolate(**kwargs), s) def test_interpolate_index_values(self): diff --git a/pandas/tests/series/methods/test_quantile.py b/pandas/tests/series/methods/test_quantile.py index 79f50afca658f..3c4372b6fb58e 100644 --- a/pandas/tests/series/methods/test_quantile.py +++ b/pandas/tests/series/methods/test_quantile.py @@ -6,6 +6,7 @@ import pandas as pd from pandas import Index, Series import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index from pandas.core.indexes.datetimes import Timestamp @@ -104,7 +105,7 @@ def test_quantile_nan(self): assert result == expected # all nan/empty - s1 = Series([], dtype=object) + s1 = create_series_with_explicit_index([], dtype=object) cases = [s1, Series([np.nan, np.nan])] for s in cases: @@ -163,8 +164,12 @@ def test_quantile_box(self, case): def test_datetime_timedelta_quantiles(self): # covers #9694 - assert pd.isna(Series([], dtype="M8[ns]").quantile(0.5)) - assert pd.isna(Series([], dtype="m8[ns]").quantile(0.5)) + assert pd.isna( + create_series_with_explicit_index([], dtype="M8[ns]").quantile(0.5) + ) + assert pd.isna( + create_series_with_explicit_index([], dtype="m8[ns]").quantile(0.5) + ) def test_quantile_nat(self): res = Series([pd.NaT, pd.NaT]).quantile(0.5) @@ -186,7 +191,7 @@ def test_quantile_sparse(self, values, dtype): def test_quantile_empty(self): # floats - s = Series([], dtype="float64") + s = create_series_with_explicit_index([], dtype="float64") res = s.quantile(0.5) assert np.isnan(res) @@ -196,7 +201,7 @@ def test_quantile_empty(self): tm.assert_series_equal(res, exp) # int - s = Series([], dtype="int64") + s = create_series_with_explicit_index([], dtype="int64") res = s.quantile(0.5) assert np.isnan(res) @@ -206,7 +211,7 @@ def test_quantile_empty(self): tm.assert_series_equal(res, exp) # datetime - s = Series([], dtype="datetime64[ns]") + s = create_series_with_explicit_index([], dtype="datetime64[ns]") res = s.quantile(0.5) assert res is pd.NaT diff --git a/pandas/tests/series/test_apply.py b/pandas/tests/series/test_apply.py index 0661828814888..5c16de0a512c4 100644 --- a/pandas/tests/series/test_apply.py +++ b/pandas/tests/series/test_apply.py @@ -10,6 +10,7 @@ from pandas import DataFrame, Index, Series, isna import pandas._testing as tm from pandas.core.base import SpecificationError +from pandas.core.construction import create_series_with_explicit_index class TestSeriesApply: @@ -519,7 +520,7 @@ def test_map_empty(self, indices): if isinstance(indices, ABCMultiIndex): pytest.skip("Initializing a Series from a MultiIndex is not supported") - s = Series(indices) + s = create_series_with_explicit_index(indices) result = s.map({}) expected = pd.Series(np.nan, index=s.index) diff --git a/pandas/tests/series/test_combine_concat.py b/pandas/tests/series/test_combine_concat.py index 0766bfc37d7ca..c409b274b6964 100644 --- a/pandas/tests/series/test_combine_concat.py +++ b/pandas/tests/series/test_combine_concat.py @@ -3,6 +3,7 @@ import pandas as pd from pandas import Series +from pandas.core.construction import create_series_with_explicit_index class TestSeriesConcat: @@ -94,7 +95,10 @@ def test_concat_empty_series_dtype_category_with_array(self): # GH 18515 assert ( pd.concat( - [Series(np.array([]), dtype="category"), Series(dtype="float64")] + [ + create_series_with_explicit_index(np.array([]), dtype="category"), + Series(dtype="float64"), + ] ).dtype == "float64" ) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index effb324298c95..6d0b7691b664f 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -28,6 +28,7 @@ ) import pandas._testing as tm from pandas.core.arrays import IntervalArray, period_array +from pandas.core.construction import create_series_with_explicit_index class TestSeriesConstructors: @@ -134,12 +135,21 @@ def test_constructor_empty(self, input_class): # With explicit dtype: empty = Series(dtype="float64") - empty2 = Series(input_class(), dtype="float64") + + if input_class is list: + with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False): + empty2 = Series(input_class(), dtype="float64") + else: + empty2 = Series(input_class(), dtype="float64") tm.assert_series_equal(empty, empty2, check_index_type=False) # GH 18515 : with dtype=category: empty = Series(dtype="category") - empty2 = Series(input_class(), dtype="category") + if input_class is list: + with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False): + empty2 = Series(input_class(), dtype="category") + else: + empty2 = Series(input_class(), dtype="category") tm.assert_series_equal(empty, empty2, check_index_type=False) if input_class is not list: @@ -1391,7 +1401,7 @@ def test_constructor_generic_timestamp_no_frequency(self, dtype): msg = "dtype has no unit. Please pass in" with pytest.raises(ValueError, match=msg): - Series([], dtype=dtype) + create_series_with_explicit_index([], dtype=dtype) @pytest.mark.parametrize( "dtype,msg", @@ -1404,7 +1414,7 @@ def test_constructor_generic_timestamp_bad_frequency(self, dtype, msg): # see gh-15524, gh-15987 with pytest.raises(TypeError, match=msg): - Series([], dtype=dtype) + create_series_with_explicit_index([], dtype=dtype) @pytest.mark.parametrize("dtype", [None, "uint8", "category"]) def test_constructor_range_dtype(self, dtype): diff --git a/pandas/tests/series/test_dtypes.py b/pandas/tests/series/test_dtypes.py index 05e708e575a64..1d02f24ea6455 100644 --- a/pandas/tests/series/test_dtypes.py +++ b/pandas/tests/series/test_dtypes.py @@ -21,6 +21,7 @@ date_range, ) import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index class TestSeriesDtypes: @@ -404,9 +405,9 @@ def test_astype_empty_constructor_equality(self, dtype): "M", "m", # Generic timestamps raise a ValueError. Already tested. ): - init_empty = Series([], dtype=dtype) + init_empty = create_series_with_explicit_index([], dtype=dtype) with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False): - as_type_empty = Series([]).astype(dtype) + as_type_empty = create_series_with_explicit_index([]).astype(dtype) tm.assert_series_equal(init_empty, as_type_empty) def test_arg_for_errors_in_astype(self): diff --git a/pandas/tests/series/test_duplicates.py b/pandas/tests/series/test_duplicates.py index 89181a08819b1..50ad0e329851e 100644 --- a/pandas/tests/series/test_duplicates.py +++ b/pandas/tests/series/test_duplicates.py @@ -3,7 +3,10 @@ from pandas import Categorical, Series import pandas._testing as tm -from pandas.core.construction import create_series_with_explicit_dtype +from pandas.core.construction import ( + create_series_with_explicit_dtype, + create_series_with_explicit_index, +) def test_nunique(): @@ -15,7 +18,7 @@ def test_nunique(): assert result == 11 # GH 18051 - s = Series(Categorical([])) + s = create_series_with_explicit_index(Categorical([])) assert s.nunique() == 0 s = Series(Categorical([np.nan])) assert s.nunique() == 0 @@ -46,7 +49,7 @@ def test_unique(): tm.assert_numpy_array_equal(result, expected) # GH 18051 - s = Series(Categorical([])) + s = create_series_with_explicit_index(Categorical([])) tm.assert_categorical_equal(s.unique(), Categorical([])) s = Series(Categorical([np.nan])) tm.assert_categorical_equal(s.unique(), Categorical([np.nan])) diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index 9e9b93a499487..76272f5aaa988 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -20,6 +20,7 @@ isna, ) import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index class TestSeriesMissingData: @@ -562,7 +563,7 @@ def test_fillna(self, datetime_series): tm.assert_series_equal(result, expected) result = s1.fillna({}) tm.assert_series_equal(result, s1) - result = s1.fillna(Series((), dtype=object)) + result = s1.fillna(create_series_with_explicit_index((), dtype=object)) tm.assert_series_equal(result, s1) result = s2.fillna(s1) tm.assert_series_equal(result, s2) @@ -677,7 +678,7 @@ def test_timedelta64_nan(self): # tm.assert_series_equal(selector, expected) def test_dropna_empty(self): - s = Series([], dtype=object) + s = create_series_with_explicit_index([], dtype=object) assert len(s.dropna()) == 0 s.dropna(inplace=True) diff --git a/pandas/tests/series/test_operators.py b/pandas/tests/series/test_operators.py index 1340f514e31ce..d5277efc3971c 100644 --- a/pandas/tests/series/test_operators.py +++ b/pandas/tests/series/test_operators.py @@ -8,6 +8,7 @@ from pandas import DataFrame, Index, Series, bdate_range import pandas._testing as tm from pandas.core import ops +from pandas.core.construction import create_series_with_explicit_index class TestSeriesLogicalOps: @@ -32,7 +33,7 @@ def test_logical_operators_bool_dtype_with_empty(self): s_tft = Series([True, False, True], index=index) s_fff = Series([False, False, False], index=index) - s_empty = Series([], dtype=object) + s_empty = create_series_with_explicit_index([], dtype=object) res = s_tft & s_empty expected = s_fff @@ -407,7 +408,7 @@ def test_logical_ops_label_based(self): # filling # vs empty - empty = Series([], dtype=object) + empty = create_series_with_explicit_index([], dtype=object) result = a & empty.copy() expected = Series([False, False, False], list("bca")) diff --git a/pandas/tests/series/test_repr.py b/pandas/tests/series/test_repr.py index 77f942a9e32ec..a21a4eaa4fa6b 100644 --- a/pandas/tests/series/test_repr.py +++ b/pandas/tests/series/test_repr.py @@ -16,6 +16,7 @@ timedelta_range, ) import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index class TestSeriesRepr: @@ -124,10 +125,10 @@ def test_repr(self, datetime_series, string_series, object_series): assert "a\n" not in repr(ser) # with empty series (#4651) - s = Series([], dtype=np.int64, name="foo") + s = create_series_with_explicit_index([], dtype=np.int64, name="foo") assert repr(s) == "Series([], Name: foo, dtype: int64)" - s = Series([], dtype=np.int64, name=None) + s = create_series_with_explicit_index([], dtype=np.int64, name=None) assert repr(s) == "Series([], dtype: int64)" def test_tidy_repr(self): diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index 5f904241da485..579869762f4d6 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -34,6 +34,7 @@ import pandas.core.algorithms as algos from pandas.core.arrays import DatetimeArray import pandas.core.common as com +from pandas.core.construction import create_series_with_explicit_index class TestFactorize: @@ -2171,7 +2172,7 @@ def test_int64_add_overflow(): class TestMode: def test_no_mode(self): - exp = Series([], dtype=np.float64) + exp = create_series_with_explicit_index([], dtype=np.float64) tm.assert_series_equal(algos.mode([]), exp) def test_mode_single(self): diff --git a/pandas/tests/test_register_accessor.py b/pandas/tests/test_register_accessor.py index d839936f731a3..d8062a6fdcfeb 100644 --- a/pandas/tests/test_register_accessor.py +++ b/pandas/tests/test_register_accessor.py @@ -4,6 +4,10 @@ import pandas as pd import pandas._testing as tm +from pandas.core.construction import ( + create_series_with_explicit_dtype, + create_series_with_explicit_index, +) @contextlib.contextmanager @@ -46,7 +50,8 @@ def test_register(obj, registrar): with ensure_removed(obj, "mine"): before = set(dir(obj)) registrar("mine")(MyAccessor) - o = obj([]) if obj is not pd.Series else obj([], dtype=object) + klass = create_series_with_explicit_dtype if obj is pd.Series else obj + o = klass([]) assert o.mine.prop == "item" after = set(dir(obj)) assert (before ^ after) == {"mine"} @@ -90,4 +95,4 @@ def __init__(self, data): raise AttributeError("whoops") with pytest.raises(AttributeError, match="whoops"): - pd.Series([], dtype=object).bad + create_series_with_explicit_index([], dtype=object).bad diff --git a/pandas/tests/test_strings.py b/pandas/tests/test_strings.py index 6260d13524da3..63a57dd559ff2 100644 --- a/pandas/tests/test_strings.py +++ b/pandas/tests/test_strings.py @@ -10,6 +10,7 @@ import pandas as pd from pandas import DataFrame, Index, MultiIndex, Series, concat, isna, notna import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index import pandas.core.strings as strings @@ -207,6 +208,7 @@ def test_api_mi_raises(self): def test_api_per_dtype(self, index_or_series, dtype, any_skipna_inferred_dtype): # one instance of parametrized fixture box = index_or_series + box = create_series_with_explicit_index if box is Series else box inferred_dtype, values = any_skipna_inferred_dtype if dtype == "category" and len(values) and values[1] is pd.NA: @@ -252,6 +254,7 @@ def test_api_per_method( # just that the methods work on the specified (inferred) dtypes, # and raise on all others box = index_or_series + box = create_series_with_explicit_index if box is Series else box # one instance of each parametrized fixture inferred_dtype, values = any_allowed_skipna_inferred_dtype @@ -351,7 +354,7 @@ def test_iter(self): assert s.dropna().values.item() == "l" def test_iter_empty(self): - ds = Series([], dtype=object) + ds = create_series_with_explicit_index([], dtype=object) i, s = 100, 1 diff --git a/pandas/tests/tools/test_to_numeric.py b/pandas/tests/tools/test_to_numeric.py index 263887a8ea36e..ba905b8afafa5 100644 --- a/pandas/tests/tools/test_to_numeric.py +++ b/pandas/tests/tools/test_to_numeric.py @@ -7,6 +7,7 @@ import pandas as pd from pandas import DataFrame, Index, Series, to_numeric import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index @pytest.fixture(params=[None, "ignore", "raise", "coerce"]) @@ -54,10 +55,10 @@ def transform_assert_equal(request): ) def test_empty(input_kwargs, result_kwargs): # see gh-16302 - ser = Series([], dtype=object) + ser = create_series_with_explicit_index([], dtype=object) result = to_numeric(ser, **input_kwargs) - expected = Series([], **result_kwargs) + expected = create_series_with_explicit_index([], **result_kwargs) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/util/test_hashing.py b/pandas/tests/util/test_hashing.py index ff29df39e1871..6d3fb66eb29e4 100644 --- a/pandas/tests/util/test_hashing.py +++ b/pandas/tests/util/test_hashing.py @@ -4,6 +4,7 @@ import pandas as pd from pandas import DataFrame, Index, MultiIndex, Series import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index from pandas.core.util.hashing import hash_tuples from pandas.util import hash_array, hash_pandas_object @@ -178,7 +179,12 @@ def test_hash_pandas_object2(series, index): @pytest.mark.parametrize( - "obj", [Series([], dtype="float64"), Series([], dtype="object"), Index([])] + "obj", + [ + create_series_with_explicit_index([], dtype="float64"), + create_series_with_explicit_index([], dtype="object"), + Index([]), + ], ) def test_hash_pandas_empty_object(obj, index): # These are by-definition the same with diff --git a/pandas/tests/window/common.py b/pandas/tests/window/common.py index 6aeada3152dbb..69c5e10b46280 100644 --- a/pandas/tests/window/common.py +++ b/pandas/tests/window/common.py @@ -5,6 +5,7 @@ from pandas import DataFrame, Series, bdate_range, notna import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index N, K = 100, 10 @@ -375,7 +376,7 @@ def check_binary_ew_min_periods(name, min_periods, A, B): assert not np.isnan(result.values[11:]).any() # check series of length 0 - empty = Series([], dtype=np.float64) + empty = create_series_with_explicit_index([], dtype=np.float64) result = ew_func(empty, empty, 50, name=name, min_periods=min_periods) tm.assert_series_equal(result, empty) diff --git a/pandas/tests/window/moments/test_moments_ewm.py b/pandas/tests/window/moments/test_moments_ewm.py index 599761259e041..c068ddbe5d82f 100644 --- a/pandas/tests/window/moments/test_moments_ewm.py +++ b/pandas/tests/window/moments/test_moments_ewm.py @@ -5,6 +5,7 @@ import pandas as pd from pandas import DataFrame, Series, concat import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index from pandas.tests.window.common import ( Base, ConsistencyBase, @@ -205,7 +206,7 @@ def test_ewm_domain_checks(self): @pytest.mark.parametrize("method", ["mean", "vol", "var"]) def test_ew_empty_series(self, method): - vals = pd.Series([], dtype=np.float64) + vals = create_series_with_explicit_index([], dtype=np.float64) ewm = vals.ewm(3) result = getattr(ewm, method)() diff --git a/pandas/tests/window/moments/test_moments_expanding.py b/pandas/tests/window/moments/test_moments_expanding.py index 9dfaecee9caeb..444e2b7d00443 100644 --- a/pandas/tests/window/moments/test_moments_expanding.py +++ b/pandas/tests/window/moments/test_moments_expanding.py @@ -6,6 +6,7 @@ from pandas import DataFrame, Index, MultiIndex, Series, isna, notna import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index from pandas.tests.window.common import ConsistencyBase @@ -209,7 +210,7 @@ def expanding_mean(x, min_periods=1): def test_expanding_apply_empty_series(self, engine_and_raw): engine, raw = engine_and_raw - ser = Series([], dtype=np.float64) + ser = create_series_with_explicit_index([], dtype=np.float64) tm.assert_series_equal( ser, ser.expanding().apply(lambda x: x.mean(), raw=raw, engine=engine) ) diff --git a/pandas/tests/window/moments/test_moments_rolling.py b/pandas/tests/window/moments/test_moments_rolling.py index 3c5352fcd997d..c57bb4b9dd49e 100644 --- a/pandas/tests/window/moments/test_moments_rolling.py +++ b/pandas/tests/window/moments/test_moments_rolling.py @@ -11,6 +11,7 @@ import pandas as pd from pandas import DataFrame, DatetimeIndex, Index, Series, isna, notna import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index from pandas.core.window.common import _flex_binary_moment from pandas.tests.window.common import Base, ConsistencyBase @@ -108,7 +109,7 @@ def test_cmov_window_corner(self): assert np.isnan(result).all() # empty - vals = pd.Series([], dtype=object) + vals = create_series_with_explicit_index([], dtype=object) result = vals.rolling(5, center=True, win_type="boxcar").mean() assert len(result) == 0 diff --git a/pandas/tests/window/test_apply.py b/pandas/tests/window/test_apply.py index 7132e64c1191c..66823488c9d90 100644 --- a/pandas/tests/window/test_apply.py +++ b/pandas/tests/window/test_apply.py @@ -5,6 +5,7 @@ from pandas import DataFrame, Series, Timestamp, date_range import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index @pytest.mark.parametrize("bad_raw", [None, 1, 0]) @@ -53,7 +54,7 @@ def f(x): def test_rolling_apply(engine_and_raw): engine, raw = engine_and_raw - expected = Series([], dtype="float64") + expected = create_series_with_explicit_index([], dtype="float64") result = expected.rolling(10).apply(lambda x: x.mean(), engine=engine, raw=raw) tm.assert_series_equal(result, expected)