From 4c8d1ea3decf9377495d618f9d13166cf43ffd07 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Mon, 17 Feb 2020 12:38:16 +0100 Subject: [PATCH 01/16] found a first working solution --- doc/source/whatsnew/v1.1.0.rst | 1 + pandas/core/series.py | 7 +++++- pandas/tests/extension/base/missing.py | 2 +- .../tests/extension/decimal/test_decimal.py | 2 +- pandas/tests/extension/test_sparse.py | 2 +- pandas/tests/groupby/test_grouping.py | 2 +- pandas/tests/io/json/test_pandas.py | 2 +- pandas/tests/reductions/test_reductions.py | 2 +- pandas/tests/reshape/test_concat.py | 25 +++++++++++++------ pandas/tests/series/test_api.py | 2 +- pandas/tests/series/test_constructors.py | 15 ++++++----- pandas/tests/series/test_missing.py | 3 ++- pandas/tests/test_algos.py | 2 +- 13 files changed, 44 insertions(+), 23 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 13827e8fc4c33..09cc7313b1eab 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -56,6 +56,7 @@ Other API changes - :meth:`Series.describe` will now show distribution percentiles for ``datetime`` dtypes, statistics ``first`` and ``last`` will now be ``min`` and ``max`` to match with numeric dtypes in :meth:`DataFrame.describe` (:issue:`30164`) - :meth:`Groupby.groups` now returns an abbreviated representation when called on large dataframes (:issue:`1135`) +- :class:`Series` constructor will default to construct an :class:`Index`, rather than an :class:`RangeIndex` when constructed with empty data, matching the behaviour of ``data=None``. Backwards incompatible API changes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/pandas/core/series.py b/pandas/core/series.py index 15fe0bb98b536..04b40bf4df342 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -299,7 +299,12 @@ def __init__( if index is None: if not is_list_like(data): data = [data] - index = ibase.default_index(len(data)) + + if is_empty_data(data): + index = Index([]) + else: + index = ibase.default_index(len(data)) + elif is_list_like(data): # a scalar numpy array is list-like but doesn't diff --git a/pandas/tests/extension/base/missing.py b/pandas/tests/extension/base/missing.py index 2393d2edcd2c6..298f448fcee8a 100644 --- a/pandas/tests/extension/base/missing.py +++ b/pandas/tests/extension/base/missing.py @@ -20,7 +20,7 @@ def test_isna(self, data_missing): # GH 21189 result = pd.Series(data_missing).drop([0, 1]).isna() expected = pd.Series([], dtype=bool) - self.assert_series_equal(result, expected) + self.assert_series_equal(result, expected, check_index_type=False) def test_dropna_array(self, data_missing): result = data_missing.dropna() diff --git a/pandas/tests/extension/decimal/test_decimal.py b/pandas/tests/extension/decimal/test_decimal.py index a78e4bb34e42a..3506593c19c35 100644 --- a/pandas/tests/extension/decimal/test_decimal.py +++ b/pandas/tests/extension/decimal/test_decimal.py @@ -86,7 +86,7 @@ def convert(x): else: right_na = right.isna() - tm.assert_series_equal(left_na, right_na) + tm.assert_series_equal(left_na, right_na, *args, **kwargs) return tm.assert_series_equal(left[~left_na], right[~right_na], *args, **kwargs) @classmethod diff --git a/pandas/tests/extension/test_sparse.py b/pandas/tests/extension/test_sparse.py index 198a228b621b4..e92359a6bdb44 100644 --- a/pandas/tests/extension/test_sparse.py +++ b/pandas/tests/extension/test_sparse.py @@ -186,7 +186,7 @@ def test_isna(self, data_missing): # GH 21189 result = pd.Series(data_missing).drop([0, 1]).isna() expected = pd.Series([], dtype=expected_dtype) - self.assert_series_equal(result, expected) + self.assert_series_equal(result, expected, check_index_type=False) def test_fillna_limit_pad(self, data_missing): with tm.assert_produces_warning(PerformanceWarning): diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index efcd22f9c0c82..e82ffb0aaa98a 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -641,7 +641,7 @@ def test_groupby_empty(self): gr = s.groupby([]) result = gr.mean() - tm.assert_series_equal(result, s) + tm.assert_series_equal(result, s, check_index_type=False) # check group properties assert len(gr.grouper.groupings) == 1 diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index f2d35bfb3b5ae..4602fe39e973e 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -678,7 +678,7 @@ def test_series_roundtrip_empty(self, orient, numpy): else: expected.index = expected.index.astype(float) - tm.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected, check_index_type=False) @pytest.mark.parametrize("numpy", [True, False]) def test_series_roundtrip_timeseries(self, orient, numpy): diff --git a/pandas/tests/reductions/test_reductions.py b/pandas/tests/reductions/test_reductions.py index 211d0d52d8357..4a0d901234fab 100644 --- a/pandas/tests/reductions/test_reductions.py +++ b/pandas/tests/reductions/test_reductions.py @@ -1090,7 +1090,7 @@ class TestSeriesMode: def test_mode_empty(self, dropna, expected): s = Series([], dtype=np.float64) result = s.mode(dropna) - tm.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected, check_index_type=False) @pytest.mark.parametrize( "dropna, data, expected", diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index 5811f3bc196a1..6ea81ae26641d 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -736,17 +736,28 @@ def test_concat_categorical_empty(self): s1 = pd.Series([], dtype="category") s2 = pd.Series([], dtype="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([s1, s2], ignore_index=True), s2, check_index_type=False + ) + tm.assert_series_equal( + s1.append(s2, ignore_index=True), s2, check_index_type=False + ) s1 = pd.Series([], dtype="category") s2 = pd.Series([], dtype="object") # 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) + result = pd.concat([s1, s2], ignore_index=True) + tm.assert_series_equal(result, s2, check_index_type=False) + + result = s1.append(s2, ignore_index=True) + tm.assert_series_equal(result, s2, check_index_type=False) + + result = pd.concat([s1, s2], ignore_index=True) + tm.assert_series_equal(result, s2, check_index_type=False) + + result = s2.append(s1, ignore_index=True) + tm.assert_series_equal(result, s2, check_index_type=False) s1 = pd.Series([], dtype="category") s2 = pd.Series([np.nan, np.nan]) @@ -2202,7 +2213,7 @@ def test_concat_empty_series_timelike(self, tz, values): } ) result = concat([first, second], axis=1) - tm.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected, check_index_type=False) def test_default_index(self): # is_series and ignore_index diff --git a/pandas/tests/series/test_api.py b/pandas/tests/series/test_api.py index 33706c00c53f4..116ab8c7d2694 100644 --- a/pandas/tests/series/test_api.py +++ b/pandas/tests/series/test_api.py @@ -497,7 +497,7 @@ async def test_tab_complete_warning(self, ip): pytest.importorskip("IPython", minversion="6.0.0") from IPython.core.completer import provisionalcompleter - code = "import pandas as pd; s = pd.Series()" + code = "import pandas as pd; s = pd.Series(dtype=object)" await ip.run_code(code) with tm.assert_produces_warning(None): with provisionalcompleter("ignore"): diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index b0d06793dbe13..f927bb8d3cba4 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -127,20 +127,20 @@ def test_constructor_empty(self, input_class): with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False): empty = Series() empty2 = Series(input_class()) - - # these are Index() and RangeIndex() which don't compare type equal - # but are just .equals - tm.assert_series_equal(empty, empty2, check_index_type=False) + tm.assert_series_equal(empty, empty2) + assert type(empty.index) is Index # With explicit dtype: empty = Series(dtype="float64") empty2 = Series(input_class(), dtype="float64") - tm.assert_series_equal(empty, empty2, check_index_type=False) + tm.assert_series_equal(empty, empty2) + assert type(empty.index) is Index # GH 18515 : with dtype=category: empty = Series(dtype="category") empty2 = Series(input_class(), dtype="category") - tm.assert_series_equal(empty, empty2, check_index_type=False) + tm.assert_series_equal(empty, empty2) + assert type(empty.index) is Index if input_class is not list: # With index: @@ -148,16 +148,19 @@ def test_constructor_empty(self, input_class): empty = Series(index=range(10)) empty2 = Series(input_class(), index=range(10)) tm.assert_series_equal(empty, empty2) + assert type(empty.index) is pd.RangeIndex # With index and dtype float64: empty = Series(np.nan, index=range(10)) empty2 = Series(input_class(), index=range(10), dtype="float64") tm.assert_series_equal(empty, empty2) + assert type(empty.index) is pd.RangeIndex # GH 19853 : with empty string, index and dtype str empty = Series("", dtype=str, index=range(3)) empty2 = Series("", index=range(3)) tm.assert_series_equal(empty, empty2) + assert type(empty.index) is pd.RangeIndex @pytest.mark.parametrize("input_arg", [np.nan, float("nan")]) def test_constructor_nan(self, input_arg): diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index 6b7d9e00a5228..2ce8a8b2cd5b9 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -1151,7 +1151,8 @@ 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() + index = pd.RangeIndex.from_range(range(0, 0)) + s = Series([], dtype=object, index=index).interpolate() tm.assert_series_equal(s.interpolate(**kwargs), s) def test_interpolate_index_values(self): diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index a1de9c435c9ba..6ce799d3b5ad5 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -2163,7 +2163,7 @@ def test_int64_add_overflow(): class TestMode: def test_no_mode(self): exp = Series([], dtype=np.float64) - tm.assert_series_equal(algos.mode([]), exp) + tm.assert_series_equal(algos.mode([]), exp, check_index_type=False) def test_mode_single(self): # GH 15714 From 7a63ac9614866819a84e3889d36ed36e7b4bc10c Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Thu, 20 Feb 2020 12:54:27 +0100 Subject: [PATCH 02/16] review comments --- pandas/core/indexes/base.py | 4 +++- pandas/core/series.py | 6 +----- pandas/tests/extension/base/missing.py | 5 +++-- pandas/tests/extension/test_sparse.py | 5 +++-- pandas/tests/groupby/test_grouping.py | 7 +++++-- pandas/tests/io/json/test_pandas.py | 5 ++--- pandas/tests/reductions/test_reductions.py | 2 +- pandas/tests/reshape/test_concat.py | 16 ++++++---------- pandas/tests/series/test_api.py | 2 +- pandas/tests/series/test_missing.py | 2 +- pandas/tests/test_algos.py | 2 +- 11 files changed, 27 insertions(+), 29 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 14ee21ea5614c..042cecb606cb6 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5580,7 +5580,9 @@ def _validate_join_method(method): def default_index(n): from pandas.core.indexes.range import RangeIndex - return RangeIndex(0, n, name=None) + if n == 0: + return Index([]) + return RangeIndex(0, n) def maybe_extract_name(name, obj, cls) -> Optional[Hashable]: diff --git a/pandas/core/series.py b/pandas/core/series.py index 04b40bf4df342..f383696541d33 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -299,11 +299,7 @@ def __init__( if index is None: if not is_list_like(data): data = [data] - - if is_empty_data(data): - index = Index([]) - else: - index = ibase.default_index(len(data)) + index = ibase.default_index(len(data)) elif is_list_like(data): diff --git a/pandas/tests/extension/base/missing.py b/pandas/tests/extension/base/missing.py index 298f448fcee8a..26ab1326594c4 100644 --- a/pandas/tests/extension/base/missing.py +++ b/pandas/tests/extension/base/missing.py @@ -19,8 +19,9 @@ def test_isna(self, data_missing): # GH 21189 result = pd.Series(data_missing).drop([0, 1]).isna() - expected = pd.Series([], dtype=bool) - self.assert_series_equal(result, expected, check_index_type=False) + index = pd.RangeIndex.from_range(range(0)) + expected = pd.Series([], dtype=bool, index=index) + self.assert_series_equal(result, expected) def test_dropna_array(self, data_missing): result = data_missing.dropna() diff --git a/pandas/tests/extension/test_sparse.py b/pandas/tests/extension/test_sparse.py index e92359a6bdb44..897a54d309ea1 100644 --- a/pandas/tests/extension/test_sparse.py +++ b/pandas/tests/extension/test_sparse.py @@ -185,8 +185,9 @@ def test_isna(self, data_missing): # GH 21189 result = pd.Series(data_missing).drop([0, 1]).isna() - expected = pd.Series([], dtype=expected_dtype) - self.assert_series_equal(result, expected, check_index_type=False) + index = pd.RangeIndex.from_range(range(0)) + expected = pd.Series([], dtype=expected_dtype, index=index) + self.assert_series_equal(result, expected) def test_fillna_limit_pad(self, data_missing): with tm.assert_produces_warning(PerformanceWarning): diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index e82ffb0aaa98a..a487bc3e6ff7b 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -638,10 +638,13 @@ 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") - gr = s.groupby([]) + expected = s.copy() + expected.index = pd.RangeIndex.from_range(range(0)) + + gr = s.groupby([]) result = gr.mean() - tm.assert_series_equal(result, s, check_index_type=False) + tm.assert_series_equal(result, expected) # check group properties assert len(gr.grouper.groupings) == 1 diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index 4602fe39e973e..d740786fa679c 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -675,10 +675,9 @@ def test_series_roundtrip_empty(self, orient, numpy): # TODO: see what causes inconsistency if orient in ("values", "records"): expected = expected.reset_index(drop=True) - else: - expected.index = expected.index.astype(float) + expected.index = expected.index.astype(float) - tm.assert_series_equal(result, expected, check_index_type=False) + tm.assert_series_equal(result, expected) @pytest.mark.parametrize("numpy", [True, False]) def test_series_roundtrip_timeseries(self, orient, numpy): diff --git a/pandas/tests/reductions/test_reductions.py b/pandas/tests/reductions/test_reductions.py index 4a0d901234fab..211d0d52d8357 100644 --- a/pandas/tests/reductions/test_reductions.py +++ b/pandas/tests/reductions/test_reductions.py @@ -1090,7 +1090,7 @@ class TestSeriesMode: def test_mode_empty(self, dropna, expected): s = Series([], dtype=np.float64) result = s.mode(dropna) - tm.assert_series_equal(result, expected, check_index_type=False) + tm.assert_series_equal(result, expected) @pytest.mark.parametrize( "dropna, data, expected", diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index 6ea81ae26641d..de97854fc14c8 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -736,28 +736,24 @@ def test_concat_categorical_empty(self): s1 = pd.Series([], dtype="category") s2 = pd.Series([], dtype="category") - tm.assert_series_equal( - pd.concat([s1, s2], ignore_index=True), s2, check_index_type=False - ) - tm.assert_series_equal( - s1.append(s2, ignore_index=True), s2, check_index_type=False - ) + tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), s2) + tm.assert_series_equal(s1.append(s2, ignore_index=True), s2) s1 = pd.Series([], dtype="category") s2 = pd.Series([], dtype="object") # different dtype => not-category result = pd.concat([s1, s2], ignore_index=True) - tm.assert_series_equal(result, s2, check_index_type=False) + tm.assert_series_equal(result, s2) result = s1.append(s2, ignore_index=True) - tm.assert_series_equal(result, s2, check_index_type=False) + tm.assert_series_equal(result, s2) result = pd.concat([s1, s2], ignore_index=True) - tm.assert_series_equal(result, s2, check_index_type=False) + tm.assert_series_equal(result, s2) result = s2.append(s1, ignore_index=True) - tm.assert_series_equal(result, s2, check_index_type=False) + tm.assert_series_equal(result, s2) s1 = pd.Series([], dtype="category") s2 = pd.Series([np.nan, np.nan]) diff --git a/pandas/tests/series/test_api.py b/pandas/tests/series/test_api.py index 116ab8c7d2694..33706c00c53f4 100644 --- a/pandas/tests/series/test_api.py +++ b/pandas/tests/series/test_api.py @@ -497,7 +497,7 @@ async def test_tab_complete_warning(self, ip): pytest.importorskip("IPython", minversion="6.0.0") from IPython.core.completer import provisionalcompleter - code = "import pandas as pd; s = pd.Series(dtype=object)" + code = "import pandas as pd; s = pd.Series()" await ip.run_code(code) with tm.assert_produces_warning(None): with provisionalcompleter("ignore"): diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index 2ce8a8b2cd5b9..fcf45f43afb26 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -1151,7 +1151,7 @@ def test_interpolate_corners(self, kwargs): s = Series([np.nan, np.nan]) tm.assert_series_equal(s.interpolate(**kwargs), s) - index = pd.RangeIndex.from_range(range(0, 0)) + index = pd.RangeIndex.from_range(range(0)) s = Series([], dtype=object, index=index).interpolate() tm.assert_series_equal(s.interpolate(**kwargs), s) diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index 6ce799d3b5ad5..a1de9c435c9ba 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -2163,7 +2163,7 @@ def test_int64_add_overflow(): class TestMode: def test_no_mode(self): exp = Series([], dtype=np.float64) - tm.assert_series_equal(algos.mode([]), exp, check_index_type=False) + tm.assert_series_equal(algos.mode([]), exp) def test_mode_single(self): # GH 15714 From 799143c9d83b0ad174442445171a4ba54f4d4c71 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Thu, 20 Feb 2020 14:12:24 +0100 Subject: [PATCH 03/16] fixing tests --- pandas/tests/extension/base/missing.py | 2 +- pandas/tests/extension/test_sparse.py | 2 +- pandas/tests/frame/test_constructors.py | 8 ++++---- pandas/tests/groupby/test_filters.py | 1 + pandas/tests/groupby/test_grouping.py | 7 ++----- pandas/tests/io/json/test_pandas.py | 7 +++++-- pandas/tests/series/test_missing.py | 2 +- 7 files changed, 15 insertions(+), 14 deletions(-) diff --git a/pandas/tests/extension/base/missing.py b/pandas/tests/extension/base/missing.py index 26ab1326594c4..a85b5ad975a63 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() - index = pd.RangeIndex.from_range(range(0)) + index = pd.RangeIndex(0) expected = pd.Series([], dtype=bool, index=index) self.assert_series_equal(result, expected) diff --git a/pandas/tests/extension/test_sparse.py b/pandas/tests/extension/test_sparse.py index 897a54d309ea1..9f43071954c8c 100644 --- a/pandas/tests/extension/test_sparse.py +++ b/pandas/tests/extension/test_sparse.py @@ -185,7 +185,7 @@ def test_isna(self, data_missing): # GH 21189 result = pd.Series(data_missing).drop([0, 1]).isna() - index = pd.RangeIndex.from_range(range(0)) + index = pd.RangeIndex(0) expected = pd.Series([], dtype=expected_dtype, index=index) self.assert_series_equal(result, expected) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 8c9b7cd060059..d5f53e44015f5 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -84,9 +84,9 @@ def test_empty_constructor(self, constructor): @pytest.mark.parametrize( "emptylike,expected_index,expected_columns", [ - ([[]], RangeIndex(1), RangeIndex(0)), - ([[], []], RangeIndex(2), RangeIndex(0)), - ([(_ for _ in [])], RangeIndex(1), RangeIndex(0)), + ([[]], RangeIndex(1), Index([])), + ([[], []], RangeIndex(2), Index([])), + ([(_ for _ in [])], RangeIndex(1), Index([])), ], ) def test_emptylike_constructor(self, emptylike, expected_index, expected_columns): @@ -337,7 +337,7 @@ def test_constructor_dict(self): # with dict of empty list and Series frame = DataFrame({"A": [], "B": []}, columns=["A", "B"]) - tm.assert_index_equal(frame.index, Index([], dtype=np.int64)) + tm.assert_index_equal(frame.index, Index([])) # GH 14381 # Dict with None value diff --git a/pandas/tests/groupby/test_filters.py b/pandas/tests/groupby/test_filters.py index c16ad812eb634..8b6c523fe8a06 100644 --- a/pandas/tests/groupby/test_filters.py +++ b/pandas/tests/groupby/test_filters.py @@ -98,6 +98,7 @@ def test_filter_out_all_groups_in_df(): res = df.groupby("a") res = res.filter(lambda x: x["b"].sum() > 5, dropna=True) expected = pd.DataFrame({"a": [], "b": []}, dtype="int64") + expected.index = pd.RangeIndex(0) tm.assert_frame_equal(expected, res) diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index a487bc3e6ff7b..32c1aaa2041a9 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -611,10 +611,7 @@ def test_list_grouper_with_nat(self): @pytest.mark.parametrize( "func,expected", [ - ( - "transform", - pd.Series(name=2, dtype=np.float64, index=pd.RangeIndex(0, 0, 1)), - ), + ("transform", pd.Series(name=2, dtype=np.float64)), ( "agg", pd.Series(name=2, dtype=np.float64, index=pd.Float64Index([], name=1)), @@ -640,7 +637,7 @@ def test_groupby_empty(self): s = pd.Series([], name="name", dtype="float64") expected = s.copy() - expected.index = pd.RangeIndex.from_range(range(0)) + expected.index = pd.RangeIndex(0) gr = s.groupby([]) result = gr.mean() diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index d740786fa679c..6ec9cbff5b422 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -255,13 +255,16 @@ def test_roundtrip_empty(self, orient, convert_axes, numpy): ) expected = self.empty_frame.copy() - # TODO: both conditions below are probably bugs + # TODO: conditions below are probably bugs if convert_axes: - expected.index = expected.index.astype(float) expected.columns = expected.columns.astype(float) + if numpy and orient == "values": expected = expected.reindex([0], axis=1).reset_index(drop=True) + if convert_axes: + expected.index = expected.index.astype(float) + tm.assert_frame_equal(result, expected) @pytest.mark.parametrize("convert_axes", [True, False]) diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index fcf45f43afb26..9d13c9248a6b0 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -1151,7 +1151,7 @@ def test_interpolate_corners(self, kwargs): s = Series([np.nan, np.nan]) tm.assert_series_equal(s.interpolate(**kwargs), s) - index = pd.RangeIndex.from_range(range(0)) + index = pd.RangeIndex(0) s = Series([], dtype=object, index=index).interpolate() tm.assert_series_equal(s.interpolate(**kwargs), s) From e4b880c1867d4e6658e73b6f4d7302c7d8147db5 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Thu, 20 Feb 2020 22:03:35 +0100 Subject: [PATCH 04/16] refactorings --- pandas/core/series.py | 1 - pandas/tests/extension/base/missing.py | 3 +-- pandas/tests/extension/test_sparse.py | 3 +-- pandas/tests/groupby/test_filters.py | 3 +-- pandas/tests/reshape/test_concat.py | 20 +++++++------------- pandas/tests/series/test_missing.py | 3 +-- 6 files changed, 11 insertions(+), 22 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index f383696541d33..15fe0bb98b536 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -300,7 +300,6 @@ def __init__( if not is_list_like(data): data = [data] index = ibase.default_index(len(data)) - elif is_list_like(data): # a scalar numpy array is list-like but doesn't diff --git a/pandas/tests/extension/base/missing.py b/pandas/tests/extension/base/missing.py index a85b5ad975a63..1049edac328e7 100644 --- a/pandas/tests/extension/base/missing.py +++ b/pandas/tests/extension/base/missing.py @@ -19,8 +19,7 @@ def test_isna(self, data_missing): # GH 21189 result = pd.Series(data_missing).drop([0, 1]).isna() - index = pd.RangeIndex(0) - expected = pd.Series([], dtype=bool, index=index) + 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_sparse.py b/pandas/tests/extension/test_sparse.py index 9f43071954c8c..a19e439919682 100644 --- a/pandas/tests/extension/test_sparse.py +++ b/pandas/tests/extension/test_sparse.py @@ -185,8 +185,7 @@ def test_isna(self, data_missing): # GH 21189 result = pd.Series(data_missing).drop([0, 1]).isna() - index = pd.RangeIndex(0) - expected = pd.Series([], dtype=expected_dtype, index=index) + 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/groupby/test_filters.py b/pandas/tests/groupby/test_filters.py index 8b6c523fe8a06..4d0feb70596b0 100644 --- a/pandas/tests/groupby/test_filters.py +++ b/pandas/tests/groupby/test_filters.py @@ -97,8 +97,7 @@ def test_filter_out_all_groups_in_df(): df = pd.DataFrame({"a": [1, 1, 2], "b": [1, 2, 0]}) res = df.groupby("a") res = res.filter(lambda x: x["b"].sum() > 5, dropna=True) - expected = pd.DataFrame({"a": [], "b": []}, dtype="int64") - expected.index = pd.RangeIndex(0) + expected = pd.DataFrame({"a": [], "b": []}, dtype="int64", index=pd.RangeIndex(0)) tm.assert_frame_equal(expected, res) diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index de97854fc14c8..31eafe24be88f 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -743,17 +743,10 @@ def test_concat_categorical_empty(self): s2 = pd.Series([], dtype="object") # different dtype => not-category - result = pd.concat([s1, s2], ignore_index=True) - tm.assert_series_equal(result, s2) - - result = s1.append(s2, ignore_index=True) - tm.assert_series_equal(result, s2) - - result = pd.concat([s1, s2], ignore_index=True) - tm.assert_series_equal(result, s2) - - result = s2.append(s1, ignore_index=True) - tm.assert_series_equal(result, s2) + 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) s1 = pd.Series([], dtype="category") s2 = pd.Series([np.nan, np.nan]) @@ -2206,10 +2199,11 @@ def test_concat_empty_series_timelike(self, tz, values): { 0: pd.Series([pd.NaT] * len(values), dtype="M8[ns]").dt.tz_localize(tz), 1: values, - } + }, + index=pd.Index(list(range(len(values))), dtype="object"), ) result = concat([first, second], axis=1) - tm.assert_frame_equal(result, expected, check_index_type=False) + tm.assert_frame_equal(result, expected) def test_default_index(self): # is_series and ignore_index diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index 9d13c9248a6b0..5386a9c077752 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -1151,8 +1151,7 @@ def test_interpolate_corners(self, kwargs): s = Series([np.nan, np.nan]) tm.assert_series_equal(s.interpolate(**kwargs), s) - index = pd.RangeIndex(0) - s = Series([], dtype=object, index=index).interpolate() + s = Series([], dtype=object, index=pd.RangeIndex(0)).interpolate() tm.assert_series_equal(s.interpolate(**kwargs), s) def test_interpolate_index_values(self): From 863916f192a4b606c1244c6eccb8b762a14fa29a Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Mon, 20 Apr 2020 10:52:09 +0200 Subject: [PATCH 05/16] updated whatsnew --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index ed1ce012ddcba..005669cf15c75 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -177,7 +177,6 @@ Other API changes - Using a :func:`pandas.api.indexers.BaseIndexer` with ``skew``, ``cov``, ``corr`` will now raise a ``NotImplementedError`` (:issue:`32865`) - Using a :func:`pandas.api.indexers.BaseIndexer` with ``count``, ``min``, ``max`` will now return correct results for any monotonic :func:`pandas.api.indexers.BaseIndexer` descendant (:issue:`32865`) - Added a :func:`pandas.api.indexers.FixedForwardWindowIndexer` class to support forward-looking windows during ``rolling`` operations. -- :class:`Series` constructor will default to construct an :class:`Index`, rather than an :class:`RangeIndex` when constructed with empty data, matching the behaviour of ``data=None``. - Backwards incompatible API changes @@ -407,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`) .. --------------------------------------------------------------------------- From 6c6a7c82371a6d8864c306af768031f4c9035870 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Mon, 20 Apr 2020 11:01:53 +0200 Subject: [PATCH 06/16] added DeprecationWarning instead of code change --- pandas/core/indexes/base.py | 2 -- pandas/core/series.py | 13 ++++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 9175caf7e7faa..35fdc87a74875 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5604,8 +5604,6 @@ def _validate_join_method(method: str): def default_index(n): from pandas.core.indexes.range import RangeIndex - if n == 0: - return Index([]) return RangeIndex(0, n) diff --git a/pandas/core/series.py b/pandas/core/series.py index 5a1d7f3b90bd9..48fc9ece0f66e 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 From b9248659598b35585e801e70dca90082f9126db7 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Mon, 20 Apr 2020 11:12:21 +0200 Subject: [PATCH 07/16] undid the changed tests --- pandas/core/indexes/base.py | 2 +- pandas/tests/extension/base/missing.py | 2 +- pandas/tests/extension/decimal/test_decimal.py | 2 +- pandas/tests/extension/test_sparse.py | 2 +- pandas/tests/frame/test_constructors.py | 8 ++++---- pandas/tests/groupby/test_filters.py | 2 +- pandas/tests/groupby/test_grouping.py | 12 ++++++------ pandas/tests/io/json/test_pandas.py | 10 ++++------ pandas/tests/reshape/test_concat.py | 3 +-- pandas/tests/series/methods/test_interpolate.py | 2 +- pandas/tests/series/test_constructors.py | 15 ++++++--------- 11 files changed, 27 insertions(+), 33 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 35fdc87a74875..b67e3347e6af0 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5604,7 +5604,7 @@ def _validate_join_method(method: str): def default_index(n): from pandas.core.indexes.range import RangeIndex - return RangeIndex(0, n) + return RangeIndex(0, n, name=None) def maybe_extract_name(name, obj, cls) -> Label: diff --git a/pandas/tests/extension/base/missing.py b/pandas/tests/extension/base/missing.py index 1049edac328e7..2393d2edcd2c6 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, index=pd.RangeIndex(0)) + expected = pd.Series([], dtype=bool) self.assert_series_equal(result, expected) def test_dropna_array(self, data_missing): diff --git a/pandas/tests/extension/decimal/test_decimal.py b/pandas/tests/extension/decimal/test_decimal.py index 974661aaa63ff..f4ffcb8d0f109 100644 --- a/pandas/tests/extension/decimal/test_decimal.py +++ b/pandas/tests/extension/decimal/test_decimal.py @@ -86,7 +86,7 @@ def convert(x): else: right_na = right.isna() - tm.assert_series_equal(left_na, right_na, *args, **kwargs) + tm.assert_series_equal(left_na, right_na) return tm.assert_series_equal(left[~left_na], right[~right_na], *args, **kwargs) @classmethod diff --git a/pandas/tests/extension/test_sparse.py b/pandas/tests/extension/test_sparse.py index a71dec28b1e29..694bbee59606f 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, index=pd.RangeIndex(0)) + expected = pd.Series([], dtype=expected_dtype) self.assert_series_equal(result, expected) def test_fillna_limit_pad(self, data_missing): diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index ce114e58ca157..baac87755c6d2 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -87,9 +87,9 @@ def test_empty_constructor(self, constructor): @pytest.mark.parametrize( "emptylike,expected_index,expected_columns", [ - ([[]], RangeIndex(1), Index([])), - ([[], []], RangeIndex(2), Index([])), - ([(_ for _ in [])], RangeIndex(1), Index([])), + ([[]], RangeIndex(1), RangeIndex(0)), + ([[], []], RangeIndex(2), RangeIndex(0)), + ([(_ for _ in [])], RangeIndex(1), RangeIndex(0)), ], ) def test_emptylike_constructor(self, emptylike, expected_index, expected_columns): @@ -342,7 +342,7 @@ def test_constructor_dict(self): # with dict of empty list and Series frame = DataFrame({"A": [], "B": []}, columns=["A", "B"]) - tm.assert_index_equal(frame.index, Index([])) + tm.assert_index_equal(frame.index, Index([], dtype=np.int64)) # GH 14381 # Dict with None value diff --git a/pandas/tests/groupby/test_filters.py b/pandas/tests/groupby/test_filters.py index 4d0feb70596b0..c16ad812eb634 100644 --- a/pandas/tests/groupby/test_filters.py +++ b/pandas/tests/groupby/test_filters.py @@ -97,7 +97,7 @@ def test_filter_out_all_groups_in_df(): df = pd.DataFrame({"a": [1, 1, 2], "b": [1, 2, 0]}) res = df.groupby("a") res = res.filter(lambda x: x["b"].sum() > 5, dropna=True) - expected = pd.DataFrame({"a": [], "b": []}, dtype="int64", index=pd.RangeIndex(0)) + expected = pd.DataFrame({"a": [], "b": []}, dtype="int64") tm.assert_frame_equal(expected, res) diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index 32c1aaa2041a9..efcd22f9c0c82 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -611,7 +611,10 @@ def test_list_grouper_with_nat(self): @pytest.mark.parametrize( "func,expected", [ - ("transform", pd.Series(name=2, dtype=np.float64)), + ( + "transform", + pd.Series(name=2, dtype=np.float64, index=pd.RangeIndex(0, 0, 1)), + ), ( "agg", pd.Series(name=2, dtype=np.float64, index=pd.Float64Index([], name=1)), @@ -635,13 +638,10 @@ 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") - - expected = s.copy() - expected.index = pd.RangeIndex(0) - gr = s.groupby([]) + result = gr.mean() - tm.assert_series_equal(result, expected) + tm.assert_series_equal(result, s) # check group properties assert len(gr.grouper.groupings) == 1 diff --git a/pandas/tests/io/json/test_pandas.py b/pandas/tests/io/json/test_pandas.py index 9f0064b20c171..0576d8e91d531 100644 --- a/pandas/tests/io/json/test_pandas.py +++ b/pandas/tests/io/json/test_pandas.py @@ -213,16 +213,13 @@ def test_roundtrip_empty(self, orient, convert_axes, numpy, empty_frame): ) expected = empty_frame.copy() - # TODO: conditions below are probably bugs + # TODO: both conditions below are probably bugs if convert_axes: + expected.index = expected.index.astype(float) expected.columns = expected.columns.astype(float) - if numpy and orient == "values": expected = expected.reindex([0], axis=1).reset_index(drop=True) - if convert_axes: - expected.index = expected.index.astype(float) - tm.assert_frame_equal(result, expected) @pytest.mark.parametrize("convert_axes", [True, False]) @@ -635,7 +632,8 @@ def test_series_roundtrip_empty(self, orient, numpy, empty_series): expected = empty_series if orient in ("values", "records"): expected = expected.reset_index(drop=True) - expected.index = expected.index.astype(float) + else: + expected.index = expected.index.astype(float) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index c5399fcdc9b3a..bccae2c4c2772 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -2203,8 +2203,7 @@ def test_concat_empty_series_timelike(self, tz, values): { 0: pd.Series([pd.NaT] * len(values), dtype="M8[ns]").dt.tz_localize(tz), 1: values, - }, - index=pd.Index(list(range(len(values))), dtype="object"), + } ) result = concat([first, second], axis=1) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/series/methods/test_interpolate.py b/pandas/tests/series/methods/test_interpolate.py index b924d15c6bf16..6844225a81a8f 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, index=pd.RangeIndex(0)).interpolate() + s = Series([], dtype=object).interpolate() tm.assert_series_equal(s.interpolate(**kwargs), s) def test_interpolate_index_values(self): diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index baa822a960cd0..effb324298c95 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -127,20 +127,20 @@ def test_constructor_empty(self, input_class): with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False): empty = Series() empty2 = Series(input_class()) - tm.assert_series_equal(empty, empty2) - assert type(empty.index) is Index + + # these are Index() and RangeIndex() which don't compare type equal + # but are just .equals + tm.assert_series_equal(empty, empty2, check_index_type=False) # With explicit dtype: empty = Series(dtype="float64") empty2 = Series(input_class(), dtype="float64") - tm.assert_series_equal(empty, empty2) - assert type(empty.index) is Index + 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") - tm.assert_series_equal(empty, empty2) - assert type(empty.index) is Index + tm.assert_series_equal(empty, empty2, check_index_type=False) if input_class is not list: # With index: @@ -148,19 +148,16 @@ def test_constructor_empty(self, input_class): empty = Series(index=range(10)) empty2 = Series(input_class(), index=range(10)) tm.assert_series_equal(empty, empty2) - assert type(empty.index) is pd.RangeIndex # With index and dtype float64: empty = Series(np.nan, index=range(10)) empty2 = Series(input_class(), index=range(10), dtype="float64") tm.assert_series_equal(empty, empty2) - assert type(empty.index) is pd.RangeIndex # GH 19853 : with empty string, index and dtype str empty = Series("", dtype=str, index=range(3)) empty2 = Series("", index=range(3)) tm.assert_series_equal(empty, empty2) - assert type(empty.index) is pd.RangeIndex @pytest.mark.parametrize("input_arg", [np.nan, float("nan")]) def test_constructor_nan(self, input_arg): From 70708b359f8eab522b5b73f671f48401773916e9 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Mon, 20 Apr 2020 12:15:04 +0200 Subject: [PATCH 08/16] silenced warnings in pandas/tests/series --- pandas/core/construction.py | 45 ++++++++++++++++++- pandas/tests/series/indexing/test_boolean.py | 7 +-- pandas/tests/series/indexing/test_indexing.py | 5 ++- .../series/methods/test_drop_duplicates.py | 5 ++- .../tests/series/methods/test_interpolate.py | 2 +- pandas/tests/series/methods/test_quantile.py | 17 ++++--- pandas/tests/series/test_apply.py | 3 +- pandas/tests/series/test_combine_concat.py | 8 +++- pandas/tests/series/test_constructors.py | 18 ++++++-- pandas/tests/series/test_dtypes.py | 5 ++- pandas/tests/series/test_duplicates.py | 9 ++-- pandas/tests/series/test_missing.py | 5 ++- pandas/tests/series/test_operators.py | 5 ++- pandas/tests/series/test_repr.py | 5 ++- 14 files changed, 106 insertions(+), 33 deletions(-) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 2d60ad9ba50bf..0711056fcace5 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -621,10 +621,51 @@ def create_series_with_explicit_dtype( ------- Series """ - from pandas.core.series import Series - if is_empty_data(data) and dtype is None: dtype = dtype_if_empty + return create_series_with_explicit_index_type( + data=data, index=index, dtype=dtype, name=name, copy=copy, fastpath=fastpath + ) + + +def create_series_with_explicit_index_type( + 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 + + if index_if_empty is None: + index_if_empty = Index([]) + + if is_list_like(data) 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/tests/series/indexing/test_boolean.py b/pandas/tests/series/indexing/test_boolean.py index e2b71b1f2f412..3dec4e3ca104a 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_type 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_type([], 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_type([], 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_type([], 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..6dbd5c766392b 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_type 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_type([], 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..3c0c89aa0fb9d 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_type @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_type(values, dtype=np.dtype(any_numpy_dtype)) + expected = create_series_with_explicit_index_type([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..7601dbddea806 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_type 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_type([], 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_type([], dtype="M8[ns]").quantile(0.5) + ) + assert pd.isna( + create_series_with_explicit_index_type([], 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_type([], 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_type([], 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_type([], 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..e5ec355665229 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_type 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_type(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..47358de8799a7 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_type class TestSeriesConcat: @@ -94,7 +95,12 @@ 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_type( + 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..274d3626ebae7 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_type 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_type([], 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_type([], 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..e54e0f3add9f1 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_type 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_type([], 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_type([]).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..bd0851d29bee2 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_type, +) def test_nunique(): @@ -15,7 +18,7 @@ def test_nunique(): assert result == 11 # GH 18051 - s = Series(Categorical([])) + s = create_series_with_explicit_index_type(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_type(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..ab538f8a01871 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_type 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_type((), 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_type([], 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..0f7119df5ecf8 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_type 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_type([], 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_type([], 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..5402dd7ee82e4 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_type 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_type([], 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_type([], dtype=np.int64, name=None) assert repr(s) == "Series([], dtype: int64)" def test_tidy_repr(self): From a1f90683d2a9d8947c61006c99dc89f62df7db64 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Mon, 20 Apr 2020 12:54:40 +0200 Subject: [PATCH 09/16] fixed linting --- pandas/core/series.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 48fc9ece0f66e..451aecb620348 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -313,9 +313,9 @@ def __init__( 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.", + "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, ) From eaeb29d1b7af1072fa134f5eb2abdaad8102e24a Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Mon, 20 Apr 2020 13:34:29 +0200 Subject: [PATCH 10/16] resolving broken tests --- pandas/core/construction.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 0711056fcace5..bcf4115ecafe1 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -621,10 +621,19 @@ def create_series_with_explicit_dtype( ------- Series """ + from pandas import RangeIndex + if is_empty_data(data) and dtype is None: dtype = dtype_if_empty + return create_series_with_explicit_index_type( - data=data, index=index, dtype=dtype, name=name, copy=copy, fastpath=fastpath + data=data, + index=index, + dtype=dtype, + name=name, + copy=copy, + fastpath=fastpath, + index_if_empty=RangeIndex(0), # non-breaking yet ) @@ -661,10 +670,11 @@ def create_series_with_explicit_index_type( """ from pandas import Index, Series # noqa: F811 + # to avoid circular imports if index_if_empty is None: index_if_empty = Index([]) - if is_list_like(data) and len(data) == 0: + if index is None and data == []: index = index_if_empty return Series( data=data, index=index, dtype=dtype, name=name, copy=copy, fastpath=fastpath From 2a20a2f6f3b73f8bc9953df79f2100ff4bddd765 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Mon, 20 Apr 2020 21:39:43 +0200 Subject: [PATCH 11/16] fixed data check in create_series_with_explicit_index + renmae of helper --- pandas/core/construction.py | 6 +++--- pandas/tests/series/indexing/test_boolean.py | 8 ++++---- pandas/tests/series/indexing/test_indexing.py | 4 ++-- .../tests/series/methods/test_drop_duplicates.py | 6 +++--- pandas/tests/series/methods/test_quantile.py | 14 +++++++------- pandas/tests/series/test_apply.py | 4 ++-- pandas/tests/series/test_combine_concat.py | 6 ++---- pandas/tests/series/test_constructors.py | 6 +++--- pandas/tests/series/test_dtypes.py | 6 +++--- pandas/tests/series/test_duplicates.py | 6 +++--- pandas/tests/series/test_missing.py | 6 +++--- pandas/tests/series/test_operators.py | 6 +++--- pandas/tests/series/test_repr.py | 6 +++--- 13 files changed, 41 insertions(+), 43 deletions(-) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index bcf4115ecafe1..a82f137442620 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -626,7 +626,7 @@ def create_series_with_explicit_dtype( if is_empty_data(data) and dtype is None: dtype = dtype_if_empty - return create_series_with_explicit_index_type( + return create_series_with_explicit_index( data=data, index=index, dtype=dtype, @@ -637,7 +637,7 @@ def create_series_with_explicit_dtype( ) -def create_series_with_explicit_index_type( +def create_series_with_explicit_index( data: Any = None, index: Optional[Union[ArrayLike, "Index"]] = None, dtype: Optional[Dtype] = None, @@ -674,7 +674,7 @@ def create_series_with_explicit_index_type( if index_if_empty is None: index_if_empty = Index([]) - if index is None and data == []: + if index is None and isinstance(data, list) 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/tests/series/indexing/test_boolean.py b/pandas/tests/series/indexing/test_boolean.py index 3dec4e3ca104a..6d7196535d230 100644 --- a/pandas/tests/series/indexing/test_boolean.py +++ b/pandas/tests/series/indexing/test_boolean.py @@ -3,7 +3,7 @@ from pandas import Index, Series import pandas._testing as tm -from pandas.core.construction import create_series_with_explicit_index_type +from pandas.core.construction import create_series_with_explicit_index from pandas.core.indexing import IndexingError from pandas.tseries.offsets import BDay @@ -21,7 +21,7 @@ def test_getitem_boolean(string_series): def test_getitem_boolean_empty(): - s = create_series_with_explicit_index_type([], 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" @@ -31,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[create_series_with_explicit_index_type([], dtype=object)] + result = s[create_series_with_explicit_index([], dtype=object)] tm.assert_series_equal(result, expected) # invalid because of the boolean indexer @@ -41,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[create_series_with_explicit_index_type([], 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 6dbd5c766392b..bc88e783cc232 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -10,7 +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_type +from pandas.core.construction import create_series_with_explicit_index from pandas.tseries.offsets import BDay @@ -618,7 +618,7 @@ def test_setitem_na(): def test_timedelta_assignment(): # GH 8209 - s = create_series_with_explicit_index_type([], 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 3c0c89aa0fb9d..255f92d8cf449 100644 --- a/pandas/tests/series/methods/test_drop_duplicates.py +++ b/pandas/tests/series/methods/test_drop_duplicates.py @@ -3,7 +3,7 @@ from pandas import Categorical, Series import pandas._testing as tm -from pandas.core.construction import create_series_with_explicit_index_type +from pandas.core.construction import create_series_with_explicit_index @pytest.mark.parametrize( @@ -47,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 = create_series_with_explicit_index_type(values, dtype=np.dtype(any_numpy_dtype)) - expected = create_series_with_explicit_index_type([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_quantile.py b/pandas/tests/series/methods/test_quantile.py index 7601dbddea806..3c4372b6fb58e 100644 --- a/pandas/tests/series/methods/test_quantile.py +++ b/pandas/tests/series/methods/test_quantile.py @@ -6,7 +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_type +from pandas.core.construction import create_series_with_explicit_index from pandas.core.indexes.datetimes import Timestamp @@ -105,7 +105,7 @@ def test_quantile_nan(self): assert result == expected # all nan/empty - s1 = create_series_with_explicit_index_type([], dtype=object) + s1 = create_series_with_explicit_index([], dtype=object) cases = [s1, Series([np.nan, np.nan])] for s in cases: @@ -165,10 +165,10 @@ def test_quantile_box(self, case): def test_datetime_timedelta_quantiles(self): # covers #9694 assert pd.isna( - create_series_with_explicit_index_type([], dtype="M8[ns]").quantile(0.5) + create_series_with_explicit_index([], dtype="M8[ns]").quantile(0.5) ) assert pd.isna( - create_series_with_explicit_index_type([], dtype="m8[ns]").quantile(0.5) + create_series_with_explicit_index([], dtype="m8[ns]").quantile(0.5) ) def test_quantile_nat(self): @@ -191,7 +191,7 @@ def test_quantile_sparse(self, values, dtype): def test_quantile_empty(self): # floats - s = create_series_with_explicit_index_type([], dtype="float64") + s = create_series_with_explicit_index([], dtype="float64") res = s.quantile(0.5) assert np.isnan(res) @@ -201,7 +201,7 @@ def test_quantile_empty(self): tm.assert_series_equal(res, exp) # int - s = create_series_with_explicit_index_type([], dtype="int64") + s = create_series_with_explicit_index([], dtype="int64") res = s.quantile(0.5) assert np.isnan(res) @@ -211,7 +211,7 @@ def test_quantile_empty(self): tm.assert_series_equal(res, exp) # datetime - s = create_series_with_explicit_index_type([], 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 e5ec355665229..5c16de0a512c4 100644 --- a/pandas/tests/series/test_apply.py +++ b/pandas/tests/series/test_apply.py @@ -10,7 +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_type +from pandas.core.construction import create_series_with_explicit_index class TestSeriesApply: @@ -520,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 = create_series_with_explicit_index_type(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 47358de8799a7..c409b274b6964 100644 --- a/pandas/tests/series/test_combine_concat.py +++ b/pandas/tests/series/test_combine_concat.py @@ -3,7 +3,7 @@ import pandas as pd from pandas import Series -from pandas.core.construction import create_series_with_explicit_index_type +from pandas.core.construction import create_series_with_explicit_index class TestSeriesConcat: @@ -96,9 +96,7 @@ def test_concat_empty_series_dtype_category_with_array(self): assert ( pd.concat( [ - create_series_with_explicit_index_type( - np.array([]), dtype="category" - ), + create_series_with_explicit_index(np.array([]), dtype="category"), Series(dtype="float64"), ] ).dtype diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 274d3626ebae7..6d0b7691b664f 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -28,7 +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_type +from pandas.core.construction import create_series_with_explicit_index class TestSeriesConstructors: @@ -1401,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): - create_series_with_explicit_index_type([], dtype=dtype) + create_series_with_explicit_index([], dtype=dtype) @pytest.mark.parametrize( "dtype,msg", @@ -1414,7 +1414,7 @@ def test_constructor_generic_timestamp_bad_frequency(self, dtype, msg): # see gh-15524, gh-15987 with pytest.raises(TypeError, match=msg): - create_series_with_explicit_index_type([], 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 e54e0f3add9f1..1d02f24ea6455 100644 --- a/pandas/tests/series/test_dtypes.py +++ b/pandas/tests/series/test_dtypes.py @@ -21,7 +21,7 @@ date_range, ) import pandas._testing as tm -from pandas.core.construction import create_series_with_explicit_index_type +from pandas.core.construction import create_series_with_explicit_index class TestSeriesDtypes: @@ -405,9 +405,9 @@ def test_astype_empty_constructor_equality(self, dtype): "M", "m", # Generic timestamps raise a ValueError. Already tested. ): - init_empty = create_series_with_explicit_index_type([], dtype=dtype) + init_empty = create_series_with_explicit_index([], dtype=dtype) with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False): - as_type_empty = create_series_with_explicit_index_type([]).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 bd0851d29bee2..50ad0e329851e 100644 --- a/pandas/tests/series/test_duplicates.py +++ b/pandas/tests/series/test_duplicates.py @@ -5,7 +5,7 @@ import pandas._testing as tm from pandas.core.construction import ( create_series_with_explicit_dtype, - create_series_with_explicit_index_type, + create_series_with_explicit_index, ) @@ -18,7 +18,7 @@ def test_nunique(): assert result == 11 # GH 18051 - s = create_series_with_explicit_index_type(Categorical([])) + s = create_series_with_explicit_index(Categorical([])) assert s.nunique() == 0 s = Series(Categorical([np.nan])) assert s.nunique() == 0 @@ -49,7 +49,7 @@ def test_unique(): tm.assert_numpy_array_equal(result, expected) # GH 18051 - s = create_series_with_explicit_index_type(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 ab538f8a01871..76272f5aaa988 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -20,7 +20,7 @@ isna, ) import pandas._testing as tm -from pandas.core.construction import create_series_with_explicit_index_type +from pandas.core.construction import create_series_with_explicit_index class TestSeriesMissingData: @@ -563,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(create_series_with_explicit_index_type((), 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) @@ -678,7 +678,7 @@ def test_timedelta64_nan(self): # tm.assert_series_equal(selector, expected) def test_dropna_empty(self): - s = create_series_with_explicit_index_type([], 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 0f7119df5ecf8..d5277efc3971c 100644 --- a/pandas/tests/series/test_operators.py +++ b/pandas/tests/series/test_operators.py @@ -8,7 +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_type +from pandas.core.construction import create_series_with_explicit_index class TestSeriesLogicalOps: @@ -33,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 = create_series_with_explicit_index_type([], dtype=object) + s_empty = create_series_with_explicit_index([], dtype=object) res = s_tft & s_empty expected = s_fff @@ -408,7 +408,7 @@ def test_logical_ops_label_based(self): # filling # vs empty - empty = create_series_with_explicit_index_type([], 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 5402dd7ee82e4..a21a4eaa4fa6b 100644 --- a/pandas/tests/series/test_repr.py +++ b/pandas/tests/series/test_repr.py @@ -16,7 +16,7 @@ timedelta_range, ) import pandas._testing as tm -from pandas.core.construction import create_series_with_explicit_index_type +from pandas.core.construction import create_series_with_explicit_index class TestSeriesRepr: @@ -125,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 = create_series_with_explicit_index_type([], 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 = create_series_with_explicit_index_type([], 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): From c9f2bd11df3f8d3619993fd6f0af29fd12c5205d Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Mon, 20 Apr 2020 22:24:30 +0200 Subject: [PATCH 12/16] fixed remaining broken test --- pandas/tests/io/parser/test_unsupported.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 From 21090122f7f7410a5a8b94eaa0b342ef4224687a Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Tue, 21 Apr 2020 00:15:32 +0200 Subject: [PATCH 13/16] silenced two warnings in the docs --- doc/source/user_guide/missing_data.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 From 96556cd6e61127ebaa269d6544a46343a1ad32ce Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Wed, 22 Apr 2020 12:05:29 +0200 Subject: [PATCH 14/16] silenced many warnings in test suite --- pandas/core/algorithms.py | 8 +++-- pandas/core/apply.py | 9 ++++-- pandas/core/construction.py | 4 ++- pandas/core/indexes/base.py | 5 ++-- pandas/core/strings.py | 8 +++-- pandas/core/tools/datetimes.py | 6 ++-- pandas/io/parsers.py | 12 ++++++-- pandas/io/pytables.py | 7 +++-- pandas/tests/arrays/boolean/test_reduction.py | 3 +- pandas/tests/arrays/integer/test_function.py | 3 +- pandas/tests/base/test_unique.py | 5 +++- pandas/tests/base/test_value_counts.py | 3 +- pandas/tests/dtypes/test_common.py | 13 +++++--- pandas/tests/dtypes/test_concat.py | 3 +- pandas/tests/dtypes/test_inference.py | 12 +++++--- pandas/tests/extension/base/missing.py | 3 +- pandas/tests/extension/test_common.py | 7 +++-- pandas/tests/extension/test_sparse.py | 3 +- pandas/tests/frame/methods/test_replace.py | 5 +++- .../tests/frame/methods/test_value_counts.py | 5 ++-- pandas/tests/frame/test_arithmetic.py | 3 +- pandas/tests/frame/test_constructors.py | 7 +++-- pandas/tests/generic/test_generic.py | 9 ++++-- pandas/tests/generic/test_to_xarray.py | 3 +- pandas/tests/groupby/test_categorical.py | 3 +- pandas/tests/indexes/common.py | 18 ++++++++--- .../tests/indexes/datetimes/test_formats.py | 5 ++-- pandas/tests/indexes/period/test_formats.py | 3 +- pandas/tests/indexes/period/test_period.py | 3 +- .../tests/indexes/timedeltas/test_formats.py | 3 +- pandas/tests/indexing/multiindex/test_loc.py | 3 +- pandas/tests/indexing/test_partial.py | 9 ++++-- pandas/tests/io/parser/test_dtypes.py | 30 ++++++++++++++----- pandas/tests/reductions/test_reductions.py | 30 +++++++++++++------ pandas/tests/resample/test_base.py | 3 +- pandas/tests/reshape/test_concat.py | 27 ++++++++++------- pandas/tests/test_algos.py | 3 +- pandas/tests/test_register_accessor.py | 10 +++++-- pandas/tests/test_strings.py | 5 +++- pandas/tests/tools/test_to_numeric.py | 5 ++-- pandas/tests/util/test_hashing.py | 8 ++++- pandas/tests/window/common.py | 3 +- .../tests/window/moments/test_moments_ewm.py | 3 +- .../window/moments/test_moments_expanding.py | 3 +- .../window/moments/test_moments_rolling.py | 3 +- pandas/tests/window/test_apply.py | 3 +- 46 files changed, 229 insertions(+), 100 deletions(-) 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 a82f137442620..fbc02f9e76bc9 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -674,7 +674,9 @@ def create_series_with_explicit_index( if index_if_empty is None: index_if_empty = Index([]) - if index is None and isinstance(data, list) and len(data) == 0: + # 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/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..62afddae5b54a 100644 --- a/pandas/tests/extension/base/missing.py +++ b/pandas/tests/extension/base/missing.py @@ -2,6 +2,7 @@ import pandas as pd import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index from .base import BaseExtensionTests @@ -19,7 +20,7 @@ def test_isna(self, data_missing): # GH 21189 result = pd.Series(data_missing).drop([0, 1]).isna() - expected = pd.Series([], dtype=bool) + expected = create_series_with_explicit_index([], dtype=bool) 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..e76761f468c4b 100644 --- a/pandas/tests/extension/test_sparse.py +++ b/pandas/tests/extension/test_sparse.py @@ -9,6 +9,7 @@ from pandas import SparseDtype import pandas._testing as tm from pandas.arrays import SparseArray +from pandas.core.construction import create_series_with_explicit_index from pandas.tests.extension import base @@ -187,7 +188,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 = create_series_with_explicit_index([], dtype=expected_dtype) 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..c4b12aae5b675 100644 --- a/pandas/tests/frame/methods/test_value_counts.py +++ b/pandas/tests/frame/methods/test_value_counts.py @@ -2,6 +2,7 @@ import pandas as pd import pandas._testing as tm +from pandas.core.construction import create_series_with_explicit_index def test_data_frame_value_counts_unsorted(): @@ -88,7 +89,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 = create_series_with_explicit_index([], dtype=np.int64) tm.assert_series_equal(result, expected) @@ -97,6 +98,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 = create_series_with_explicit_index([], dtype=np.float64) 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/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..2ae408fca02b5 100644 --- a/pandas/tests/io/parser/test_dtypes.py +++ b/pandas/tests/io/parser/test_dtypes.py @@ -15,6 +15,7 @@ import pandas as pd from pandas import Categorical, DataFrame, Index, MultiIndex, Series, 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"), + Series([], 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"), + Series([], 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/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..bcc83648fb3a9 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,14 +736,14 @@ 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") tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), s2) tm.assert_series_equal(s1.append(s2, ignore_index=True), s2) - 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") # different dtype => not-category tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), s2) @@ -748,7 +751,7 @@ 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") + s1 = create_series_with_explicit_index([], dtype="category") s2 = pd.Series([np.nan, np.nan]) # empty Series is ignored @@ -2195,13 +2198,15 @@ 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) 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, } ) @@ -2595,7 +2600,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/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..af8525cb54e32 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 @@ -37,7 +41,7 @@ def method(self): @pytest.mark.parametrize( "obj, registrar", [ - (pd.Series, pd.api.extensions.register_series_accessor), + (create_series_with_explicit_dtype, pd.api.extensions.register_series_accessor), (pd.DataFrame, pd.api.extensions.register_dataframe_accessor), (pd.Index, pd.api.extensions.register_index_accessor), ], @@ -46,7 +50,7 @@ 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) + o = obj([]) assert o.mine.prop == "item" after = set(dir(obj)) assert (before ^ after) == {"mine"} @@ -90,4 +94,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) From b1c08a68c333fe30b32d743739b925ddd6197fc9 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Wed, 22 Apr 2020 16:22:00 +0200 Subject: [PATCH 15/16] fixed tests --- pandas/tests/extension/base/missing.py | 3 +-- pandas/tests/extension/test_sparse.py | 3 +-- .../tests/frame/methods/test_value_counts.py | 5 ++--- pandas/tests/reshape/test_concat.py | 19 +++++++++++-------- pandas/tests/test_register_accessor.py | 5 +++-- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/pandas/tests/extension/base/missing.py b/pandas/tests/extension/base/missing.py index 62afddae5b54a..1049edac328e7 100644 --- a/pandas/tests/extension/base/missing.py +++ b/pandas/tests/extension/base/missing.py @@ -2,7 +2,6 @@ import pandas as pd import pandas._testing as tm -from pandas.core.construction import create_series_with_explicit_index from .base import BaseExtensionTests @@ -20,7 +19,7 @@ def test_isna(self, data_missing): # GH 21189 result = pd.Series(data_missing).drop([0, 1]).isna() - expected = create_series_with_explicit_index([], 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_sparse.py b/pandas/tests/extension/test_sparse.py index e76761f468c4b..a71dec28b1e29 100644 --- a/pandas/tests/extension/test_sparse.py +++ b/pandas/tests/extension/test_sparse.py @@ -9,7 +9,6 @@ from pandas import SparseDtype import pandas._testing as tm from pandas.arrays import SparseArray -from pandas.core.construction import create_series_with_explicit_index from pandas.tests.extension import base @@ -188,7 +187,7 @@ def test_isna(self, data_missing): # GH 21189 result = pd.Series(data_missing).drop([0, 1]).isna() - expected = create_series_with_explicit_index([], 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_value_counts.py b/pandas/tests/frame/methods/test_value_counts.py index c4b12aae5b675..4b2e8148c2ddb 100644 --- a/pandas/tests/frame/methods/test_value_counts.py +++ b/pandas/tests/frame/methods/test_value_counts.py @@ -2,7 +2,6 @@ import pandas as pd import pandas._testing as tm -from pandas.core.construction import create_series_with_explicit_index def test_data_frame_value_counts_unsorted(): @@ -89,7 +88,7 @@ def test_data_frame_value_counts_empty(): df_no_cols = pd.DataFrame() result = df_no_cols.value_counts() - expected = create_series_with_explicit_index([], dtype=np.int64) + expected = pd.Series([], dtype=np.int64, index=pd.RangeIndex(0)) tm.assert_series_equal(result, expected) @@ -98,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 = create_series_with_explicit_index([], dtype=np.float64) + expected = pd.Series([], dtype=np.float64, index=pd.RangeIndex(0)) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index bcc83648fb3a9..cb41a1403444c 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -738,18 +738,20 @@ def test_concat_categorical_empty(self): 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 = 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 = create_series_with_explicit_index([], dtype="category") s2 = pd.Series([np.nan, np.nan]) @@ -2201,6 +2203,7 @@ def test_concat_empty_series_timelike(self, tz, values): first = create_series_with_explicit_index([], dtype="M8[ns]").dt.tz_localize(tz) dtype = None if values else np.float64 second = create_series_with_explicit_index(values, dtype=dtype) + result = concat([first, second], axis=1) expected = DataFrame( { @@ -2208,9 +2211,9 @@ def test_concat_empty_series_timelike(self, tz, values): [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): diff --git a/pandas/tests/test_register_accessor.py b/pandas/tests/test_register_accessor.py index af8525cb54e32..d8062a6fdcfeb 100644 --- a/pandas/tests/test_register_accessor.py +++ b/pandas/tests/test_register_accessor.py @@ -41,7 +41,7 @@ def method(self): @pytest.mark.parametrize( "obj, registrar", [ - (create_series_with_explicit_dtype, pd.api.extensions.register_series_accessor), + (pd.Series, pd.api.extensions.register_series_accessor), (pd.DataFrame, pd.api.extensions.register_dataframe_accessor), (pd.Index, pd.api.extensions.register_index_accessor), ], @@ -50,7 +50,8 @@ def test_register(obj, registrar): with ensure_removed(obj, "mine"): before = set(dir(obj)) registrar("mine")(MyAccessor) - o = obj([]) + 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"} From 6d30bce6ef443cd8fef0c30f2fb4c560c09841da Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Wed, 22 Apr 2020 22:24:34 +0200 Subject: [PATCH 16/16] silenced some more warnings --- pandas/tests/groupby/test_grouping.py | 6 ++++-- pandas/tests/io/parser/test_dtypes.py | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) 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/io/parser/test_dtypes.py b/pandas/tests/io/parser/test_dtypes.py index 2ae408fca02b5..12f03c3704c6c 100644 --- a/pandas/tests/io/parser/test_dtypes.py +++ b/pandas/tests/io/parser/test_dtypes.py @@ -13,7 +13,7 @@ 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 @@ -435,7 +435,7 @@ def test_empty_with_dup_column_pass_dtype_by_indexes(all_parsers): expected = concat( [ create_series_with_explicit_index([], name="one", dtype="u1"), - Series([], name="one.1", dtype="f"), + create_series_with_explicit_index([], name="one.1", dtype="f"), ], axis=1, ) @@ -452,7 +452,7 @@ def test_empty_with_dup_column_pass_dtype_by_indexes_raises(all_parsers): expected = concat( [ create_series_with_explicit_index([], name="one", dtype="u1"), - Series([], name="one.1", dtype="f"), + create_series_with_explicit_index([], name="one.1", dtype="f"), ], axis=1, )