Skip to content

TST: astyp via loc to int64 #36942

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

Closed
Closed
Changes from 3 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
10 changes: 10 additions & 0 deletions pandas/tests/frame/methods/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest

from pandas import (
NA,
Categorical,
CategoricalDtype,
DataFrame,
Expand Down Expand Up @@ -136,6 +137,15 @@ def test_astype_cast_nan_inf_int(self, val, dtype):
with pytest.raises(ValueError, match=msg):
df.astype(dtype)

@pytest.mark.parametrize("dtype", ["Int64", "Int32", "Int16"])
@pytest.mark.parametrize("val", [np.nan, NA])
Copy link
Contributor

Choose a reason for hiding this comment

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

use nulls_fixture instead and any_nullable_int_dtype fixture

Copy link
Contributor Author

@hardikpnsp hardikpnsp Oct 11, 2020

Choose a reason for hiding this comment

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

@jreback , here in nulls_fixture, the test case fails for pd.NaT because it is inferred as datetime64. I think it is expected behavior but would like your opinion on it.

code snippet for reference

>>> df = pd.DataFrame({"a": ["foo"], "b": [pd.NaT]})
>>> df.dtypes
a            object
b    datetime64[ns]
dtype: object
>>> df.loc[:, "b"] = df.loc[:, "b"].astype('Int64')
TypeError: datetime64[ns] cannot be converted to an IntegerDtype

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@arw2019 @jreback , Any updates on this one? would appreciate some help and direction.

Copy link
Member

Choose a reason for hiding this comment

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

Could use pytest.skipif for the datetime case
or test for the TypeError somehow but I'm not sure that's necessary

def test_astype_cast_via_loc_nan_int(self, val, dtype):
# GH31861
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 add the working conditions as well (e.g. nan + a value) from the OP

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure! let me try.

expected = DataFrame({"a": ["foo"], "b": integer_array([val], dtype=dtype)})
df = DataFrame({"a": ["foo"], "b": [val]})
df.loc[:, "b"] = df.loc[:, "b"].astype(dtype)
tm.assert_frame_equal(df, expected)

def test_astype_str(self):
# see GH#9757
a = Series(date_range("2010-01-04", periods=5))
Expand Down