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

Conversation

arw2019
Copy link
Member

@arw2019 arw2019 commented Nov 21, 2020

@arw2019 arw2019 closed this Nov 21, 2020
@arw2019 arw2019 reopened this Nov 21, 2020
@jreback jreback added ExtensionArray Extending pandas with custom dtypes or arrays. Testing pandas testing functions or related to the test suite labels Nov 21, 2020
@jbrockmendel
Copy link
Member

this looks fine. should there be an analogous test in tests.extension?

@arw2019
Copy link
Member Author

arw2019 commented Nov 23, 2020

this looks fine. should there be an analogous test in tests.extension?

Hmmm do you mean something like this?

In [7]: pd.array(pd.Interval(0, 1))
(...)
ValueError: Cannot pass scalar '(0, 1]' to 'pandas.array'.

We test this here:

def test_scalar_raises():
with pytest.raises(ValueError, match="Cannot pass scalar '1'"):
pd.array(1)

and
@pytest.mark.parametrize(
"values",
[
["foo", "bar"],
["1", "2"],
"foo",
1,
1.0,
pd.date_range("20130101", periods=2),
np.array(["foo"]),
[[1, 2], [3, 4]],
[np.nan, {"a": 1}],
],
)
def test_to_array_error(values):
# error in converting existing arrays to FloatingArray
msg = (
r"(:?.* cannot be converted to a FloatingDtype)"
r"|(:?values must be a 1D list-like)"
r"|(:?Cannot pass scalar)"
)
with pytest.raises((TypeError, ValueError), match=msg):
pd.array(values, dtype="Float64")

@jreback jreback added this to the 1.2 milestone Nov 24, 2020
@@ -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")))
Copy link
Contributor

Choose a reason for hiding this comment

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

can you create a fixture of the ea_scalars and use it here: https://github.com/pandas-dev/pandas/blob/master/pandas/tests/frame/test_constructors.py#L738 and in the new test you are adding

Copy link
Contributor

Choose a reason for hiding this comment

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

there are also similar tests for a dict of a scalar in both series/frame which could be updated to use the fixture as well

Copy link
Member Author

Choose a reason for hiding this comment

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

can you create a fixture of the ea_scalars and use it here: https://github.com/pandas-dev/pandas/blob/master/pandas/tests/frame/test_constructors.py#L738 and in the new test you are adding

Done

@@ -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")))
Copy link
Contributor

Choose a reason for hiding this comment

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

there are also similar tests for a dict of a scalar in both series/frame which could be updated to use the fixture as well

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!

Timestamp("2011-01-01", tz="US/Eastern"),
DatetimeTZDtype(tz="US/Eastern"),
),
(Timedelta(seconds=500), "timedelta64[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!

@jreback jreback merged commit 7ceacb4 into pandas-dev:master Nov 24, 2020
@jreback
Copy link
Contributor

jreback commented Nov 24, 2020

thanks @arw2019

FYI there maybe other places where we can use this fixture, if its simple, would take a PR, if not, pls open an issue.

@arw2019
Copy link
Member Author

arw2019 commented Nov 27, 2020

FYI there maybe other places where we can use this fixture, if its simple, would take a PR, if not, pls open an issue.

Working on it. Probably will be a single PR but will see

@arw2019 arw2019 deleted the GH28401 branch November 27, 2020 05:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ExtensionArray Extending pandas with custom dtypes or arrays. Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Constructing a Series from a scalar generally doesn't work for extension types
3 participants