Skip to content

Commit

Permalink
TST: Series construction from ExtensionDtype scalar (#37989)
Browse files Browse the repository at this point in the history
  • Loading branch information
arw2019 authored Nov 24, 2020
1 parent 99376b3 commit 7ceacb4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 36 deletions.
24 changes: 23 additions & 1 deletion pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@

import pandas.util._test_decorators as td

from pandas.core.dtypes.dtypes import DatetimeTZDtype, IntervalDtype

import pandas as pd
from pandas import DataFrame, Series
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
Expand Down Expand Up @@ -687,6 +689,26 @@ def float_frame():
return DataFrame(tm.getSeriesData())


# ----------------------------------------------------------------
# Scalars
# ----------------------------------------------------------------
@pytest.fixture(
params=[
(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"),
),
(Timedelta(seconds=500), "timedelta64[ns]"),
]
)
def ea_scalar_and_dtype(request):
return request.param


# ----------------------------------------------------------------
# Operators & Operations
# ----------------------------------------------------------------
Expand Down
19 changes: 5 additions & 14 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
38 changes: 17 additions & 21 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -95,6 +90,17 @@ 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_and_dtype):
# GH 28401

ea_scalar, ea_dtype = ea_scalar_and_dtype

ser = Series(ea_scalar, index=range(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):
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
empty_series = Series()
Expand Down Expand Up @@ -1107,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)

Expand Down

0 comments on commit 7ceacb4

Please sign in to comment.