Skip to content

Commit 7ceacb4

Browse files
authored
TST: Series construction from ExtensionDtype scalar (#37989)
1 parent 99376b3 commit 7ceacb4

File tree

3 files changed

+45
-36
lines changed

3 files changed

+45
-36
lines changed

pandas/conftest.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@
3333

3434
import pandas.util._test_decorators as td
3535

36+
from pandas.core.dtypes.dtypes import DatetimeTZDtype, IntervalDtype
37+
3638
import pandas as pd
37-
from pandas import DataFrame, Series
39+
from pandas import DataFrame, Interval, Period, Series, Timedelta, Timestamp
3840
import pandas._testing as tm
3941
from pandas.core import ops
4042
from pandas.core.indexes.api import Index, MultiIndex
@@ -687,6 +689,26 @@ def float_frame():
687689
return DataFrame(tm.getSeriesData())
688690

689691

692+
# ----------------------------------------------------------------
693+
# Scalars
694+
# ----------------------------------------------------------------
695+
@pytest.fixture(
696+
params=[
697+
(Interval(left=0, right=5), IntervalDtype("int64")),
698+
(Interval(left=0.1, right=0.5), IntervalDtype("float64")),
699+
(Period("2012-01", freq="M"), "period[M]"),
700+
(Period("2012-02-01", freq="D"), "period[D]"),
701+
(
702+
Timestamp("2011-01-01", tz="US/Eastern"),
703+
DatetimeTZDtype(tz="US/Eastern"),
704+
),
705+
(Timedelta(seconds=500), "timedelta64[ns]"),
706+
]
707+
)
708+
def ea_scalar_and_dtype(request):
709+
return request.param
710+
711+
690712
# ----------------------------------------------------------------
691713
# Operators & Operations
692714
# ----------------------------------------------------------------

pandas/tests/frame/test_constructors.py

+5-14
Original file line numberDiff line numberDiff line change
@@ -717,21 +717,12 @@ def test_constructor_period_dict(self):
717717
assert df["a"].dtype == a.dtype
718718
assert df["b"].dtype == b.dtype
719719

720-
@pytest.mark.parametrize(
721-
"data,dtype",
722-
[
723-
(Period("2012-01", freq="M"), "period[M]"),
724-
(Period("2012-02-01", freq="D"), "period[D]"),
725-
(Interval(left=0, right=5), IntervalDtype("int64")),
726-
(Interval(left=0.1, right=0.5), IntervalDtype("float64")),
727-
],
728-
)
729-
def test_constructor_period_dict_scalar(self, data, dtype):
730-
# scalar periods
731-
df = DataFrame({"a": data}, index=[0])
732-
assert df["a"].dtype == dtype
720+
def test_constructor_dict_extension_scalar(self, ea_scalar_and_dtype):
721+
ea_scalar, ea_dtype = ea_scalar_and_dtype
722+
df = DataFrame({"a": ea_scalar}, index=[0])
723+
assert df["a"].dtype == ea_dtype
733724

734-
expected = DataFrame(index=[0], columns=["a"], data=data)
725+
expected = DataFrame(index=[0], columns=["a"], data=ea_scalar)
735726

736727
tm.assert_frame_equal(df, expected)
737728

pandas/tests/series/test_constructors.py

+17-21
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@
99
from pandas._libs import iNaT, lib
1010

1111
from pandas.core.dtypes.common import is_categorical_dtype, is_datetime64tz_dtype
12-
from pandas.core.dtypes.dtypes import (
13-
CategoricalDtype,
14-
DatetimeTZDtype,
15-
IntervalDtype,
16-
PeriodDtype,
17-
)
12+
from pandas.core.dtypes.dtypes import CategoricalDtype
1813

1914
import pandas as pd
2015
from pandas import (
@@ -95,6 +90,17 @@ def test_scalar_conversion(self):
9590
assert float(Series([1.0])) == 1.0
9691
assert int(Series([1.0])) == 1
9792

93+
def test_scalar_extension_dtype(self, ea_scalar_and_dtype):
94+
# GH 28401
95+
96+
ea_scalar, ea_dtype = ea_scalar_and_dtype
97+
98+
ser = Series(ea_scalar, index=range(3))
99+
expected = Series([ea_scalar] * 3, dtype=ea_dtype)
100+
101+
assert ser.dtype == ea_dtype
102+
tm.assert_series_equal(ser, expected)
103+
98104
def test_constructor(self, datetime_series):
99105
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
100106
empty_series = Series()
@@ -1107,23 +1113,13 @@ def test_constructor_dict_order(self):
11071113
expected = Series([1, 0, 2], index=list("bac"))
11081114
tm.assert_series_equal(result, expected)
11091115

1110-
@pytest.mark.parametrize(
1111-
"data,dtype",
1112-
[
1113-
(Period("2020-01"), PeriodDtype("M")),
1114-
(Interval(left=0, right=5), IntervalDtype("int64")),
1115-
(
1116-
Timestamp("2011-01-01", tz="US/Eastern"),
1117-
DatetimeTZDtype(tz="US/Eastern"),
1118-
),
1119-
],
1120-
)
1121-
def test_constructor_dict_extension(self, data, dtype):
1122-
d = {"a": data}
1116+
def test_constructor_dict_extension(self, ea_scalar_and_dtype):
1117+
ea_scalar, ea_dtype = ea_scalar_and_dtype
1118+
d = {"a": ea_scalar}
11231119
result = Series(d, index=["a"])
1124-
expected = Series(data, index=["a"], dtype=dtype)
1120+
expected = Series(ea_scalar, index=["a"], dtype=ea_dtype)
11251121

1126-
assert result.dtype == dtype
1122+
assert result.dtype == ea_dtype
11271123

11281124
tm.assert_series_equal(result, expected)
11291125

0 commit comments

Comments
 (0)