From 4c4b9d7603ca3f94296ddeeba9f90c61f979fc98 Mon Sep 17 00:00:00 2001 From: arw2019 Date: Sat, 21 Nov 2020 19:46:36 +0000 Subject: [PATCH 1/6] add tests --- pandas/tests/series/test_constructors.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index debd516da9eec..496111b670b41 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -95,6 +95,14 @@ def test_scalar_conversion(self): assert float(Series([1.0])) == 1.0 assert int(Series([1.0])) == 1 + @pytest.mark.parametrize("scalar", (Interval(0, 1), Period("2019Q1", freq="Q"))) + def test_scalar_extension_dtype(self, scalar): + # GH 28401 + + ser = Series(scalar, index=range(3)) + expected = Series([scalar] * 3) + tm.assert_series_equal(ser, expected) + def test_constructor(self, datetime_series): with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False): empty_series = Series() From d219ccdf874fec9570557bfb9aeb2bb2073d7d16 Mon Sep 17 00:00:00 2001 From: Andrew Wieteska Date: Mon, 23 Nov 2020 22:46:44 -0500 Subject: [PATCH 2/6] create ea_scalar fixture --- pandas/conftest.py | 10 +++++++++- pandas/tests/series/test_constructors.py | 7 +++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/pandas/conftest.py b/pandas/conftest.py index 77e9af67590a6..ec8dfd2309dc3 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -34,7 +34,7 @@ import pandas.util._test_decorators as td import pandas as pd -from pandas import DataFrame, Series +from pandas import DataFrame, Interval, Period, Series import pandas._testing as tm from pandas.core import ops from pandas.core.indexes.api import Index, MultiIndex @@ -687,6 +687,14 @@ def float_frame(): return DataFrame(tm.getSeriesData()) +# ---------------------------------------------------------------- +# Scalars +# ---------------------------------------------------------------- +@pytest.fixture(params=[Interval(0, 1), Period("2019Q1", freq="Q")]) +def ea_scalar(request): + return request.param + + # ---------------------------------------------------------------- # Operators & Operations # ---------------------------------------------------------------- diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 496111b670b41..eefd577d36a55 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -95,12 +95,11 @@ def test_scalar_conversion(self): assert float(Series([1.0])) == 1.0 assert int(Series([1.0])) == 1 - @pytest.mark.parametrize("scalar", (Interval(0, 1), Period("2019Q1", freq="Q"))) - def test_scalar_extension_dtype(self, scalar): + def test_scalar_extension_dtype(self, ea_scalar): # GH 28401 - ser = Series(scalar, index=range(3)) - expected = Series([scalar] * 3) + ser = Series(ea_scalar, index=range(3)) + expected = Series([ea_scalar] * 3) tm.assert_series_equal(ser, expected) def test_constructor(self, datetime_series): From 6fc2c5bb27ca7096194a5332322521b674d0af72 Mon Sep 17 00:00:00 2001 From: Andrew Wieteska Date: Mon, 23 Nov 2020 23:14:39 -0500 Subject: [PATCH 3/6] rewrite ser dict tests --- pandas/conftest.py | 17 ++++++++++-- pandas/tests/series/test_constructors.py | 35 ++++++++---------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/pandas/conftest.py b/pandas/conftest.py index ec8dfd2309dc3..84f0264f5bd3b 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -33,8 +33,10 @@ import pandas.util._test_decorators as td +from pandas.core.dtypes.dtypes import DatetimeTZDtype + import pandas as pd -from pandas import DataFrame, Interval, Period, Series +from pandas import DataFrame, Interval, Period, Series, Timestamp import pandas._testing as tm from pandas.core import ops from pandas.core.indexes.api import Index, MultiIndex @@ -690,8 +692,17 @@ def float_frame(): # ---------------------------------------------------------------- # Scalars # ---------------------------------------------------------------- -@pytest.fixture(params=[Interval(0, 1), Period("2019Q1", freq="Q")]) -def ea_scalar(request): +@pytest.fixture( + params=[ + (Interval(0, 1), "interval[int64]"), + (Period("2019Q1", freq="Q"), "period[Q-DEC]"), + ( + Timestamp("2011-01-01", tz="US/Eastern"), + DatetimeTZDtype(tz="US/Eastern"), + ), + ] +) +def ea_scalar_and_dtype(request): return request.param diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index eefd577d36a55..d790a85c94193 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -9,12 +9,7 @@ from pandas._libs import iNaT, lib from pandas.core.dtypes.common import is_categorical_dtype, is_datetime64tz_dtype -from pandas.core.dtypes.dtypes import ( - CategoricalDtype, - DatetimeTZDtype, - IntervalDtype, - PeriodDtype, -) +from pandas.core.dtypes.dtypes import CategoricalDtype import pandas as pd from pandas import ( @@ -95,11 +90,15 @@ def test_scalar_conversion(self): assert float(Series([1.0])) == 1.0 assert int(Series([1.0])) == 1 - def test_scalar_extension_dtype(self, ea_scalar): + def test_scalar_extension_dtype(self, ea_scalar_and_dtype): # GH 28401 + ea_scalar, ea_dtype = ea_scalar_and_dtype + ser = Series(ea_scalar, index=range(3)) - expected = Series([ea_scalar] * 3) + expected = Series([ea_scalar] * 3, dtype=ea_dtype) + + assert ser.dtype == ea_dtype tm.assert_series_equal(ser, expected) def test_constructor(self, datetime_series): @@ -1114,23 +1113,13 @@ def test_constructor_dict_order(self): expected = Series([1, 0, 2], index=list("bac")) tm.assert_series_equal(result, expected) - @pytest.mark.parametrize( - "data,dtype", - [ - (Period("2020-01"), PeriodDtype("M")), - (Interval(left=0, right=5), IntervalDtype("int64")), - ( - Timestamp("2011-01-01", tz="US/Eastern"), - DatetimeTZDtype(tz="US/Eastern"), - ), - ], - ) - def test_constructor_dict_extension(self, data, dtype): - d = {"a": data} + def test_constructor_dict_extension(self, ea_scalar_and_dtype): + ea_scalar, ea_dtype = ea_scalar_and_dtype + d = {"a": ea_scalar} result = Series(d, index=["a"]) - expected = Series(data, index=["a"], dtype=dtype) + expected = Series(ea_scalar, index=["a"], dtype=ea_dtype) - assert result.dtype == dtype + assert result.dtype == ea_dtype tm.assert_series_equal(result, expected) From 886d57532775efc098a12182be17249e7aeab4fc Mon Sep 17 00:00:00 2001 From: Andrew Wieteska Date: Mon, 23 Nov 2020 23:19:53 -0500 Subject: [PATCH 4/6] add timedelta scalar --- pandas/conftest.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/conftest.py b/pandas/conftest.py index 84f0264f5bd3b..9547ee8123630 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -36,7 +36,7 @@ from pandas.core.dtypes.dtypes import DatetimeTZDtype import pandas as pd -from pandas import DataFrame, Interval, Period, Series, Timestamp +from pandas import DataFrame, Interval, Period, Series, Timedelta, Timestamp import pandas._testing as tm from pandas.core import ops from pandas.core.indexes.api import Index, MultiIndex @@ -700,6 +700,7 @@ def float_frame(): Timestamp("2011-01-01", tz="US/Eastern"), DatetimeTZDtype(tz="US/Eastern"), ), + (Timedelta(seconds=500), "timedelta64[ns]"), ] ) def ea_scalar_and_dtype(request): From 2440f10fbfb164845f30b1fdb7ae14aa91cc652d Mon Sep 17 00:00:00 2001 From: Andrew Wieteska Date: Tue, 24 Nov 2020 00:03:15 -0500 Subject: [PATCH 5/6] frame dict constructor tests --- pandas/tests/frame/test_constructors.py | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 27c12aa4fb3d1..d32ca454b5fb2 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -717,21 +717,12 @@ def test_constructor_period_dict(self): assert df["a"].dtype == a.dtype assert df["b"].dtype == b.dtype - @pytest.mark.parametrize( - "data,dtype", - [ - (Period("2012-01", freq="M"), "period[M]"), - (Period("2012-02-01", freq="D"), "period[D]"), - (Interval(left=0, right=5), IntervalDtype("int64")), - (Interval(left=0.1, right=0.5), IntervalDtype("float64")), - ], - ) - def test_constructor_period_dict_scalar(self, data, dtype): - # scalar periods - df = DataFrame({"a": data}, index=[0]) - assert df["a"].dtype == dtype + def test_constructor_dict_extension_scalar(self, ea_scalar_and_dtype): + ea_scalar, ea_dtype = ea_scalar_and_dtype + df = DataFrame({"a": ea_scalar}, index=[0]) + assert df["a"].dtype == ea_dtype - expected = DataFrame(index=[0], columns=["a"], data=data) + expected = DataFrame(index=[0], columns=["a"], data=ea_scalar) tm.assert_frame_equal(df, expected) From dc7d405424d87cbda836dd61620ff7e4a5a804b6 Mon Sep 17 00:00:00 2001 From: Andrew Wieteska Date: Tue, 24 Nov 2020 00:03:49 -0500 Subject: [PATCH 6/6] add float interval scalar to fixture --- pandas/conftest.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas/conftest.py b/pandas/conftest.py index 9547ee8123630..12682a68fe177 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -33,7 +33,7 @@ import pandas.util._test_decorators as td -from pandas.core.dtypes.dtypes import DatetimeTZDtype +from pandas.core.dtypes.dtypes import DatetimeTZDtype, IntervalDtype import pandas as pd from pandas import DataFrame, Interval, Period, Series, Timedelta, Timestamp @@ -694,8 +694,10 @@ def float_frame(): # ---------------------------------------------------------------- @pytest.fixture( params=[ - (Interval(0, 1), "interval[int64]"), - (Period("2019Q1", freq="Q"), "period[Q-DEC]"), + (Interval(left=0, right=5), IntervalDtype("int64")), + (Interval(left=0.1, right=0.5), IntervalDtype("float64")), + (Period("2012-01", freq="M"), "period[M]"), + (Period("2012-02-01", freq="D"), "period[D]"), ( Timestamp("2011-01-01", tz="US/Eastern"), DatetimeTZDtype(tz="US/Eastern"),