Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TST: Series construction from ExtensionDtype scalar #37989

Merged
merged 8 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]"),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xref #38032 for a weird bug when the timedelta is in ns

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great thanks. testing catching edge cases, who would think!

]
)
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