Skip to content

Add simple test for GH 28448 #29269

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

Merged
merged 3 commits into from
Oct 31, 2019
Merged

Conversation

MarcoGorelli
Copy link
Member

@MarcoGorelli MarcoGorelli commented Oct 29, 2019

],
)
def test_consistent_casting(self, data, dtype, expected):
# GH 28448
Copy link
Member

Choose a reason for hiding this comment

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

The OP uses asarray which is different than what is being tested here. Can you not do asarray in the test? The return type should be a date time array

Copy link
Member Author

@MarcoGorelli MarcoGorelli Oct 29, 2019

Choose a reason for hiding this comment

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

As in, pandas.arrays.DatetimeArray? pd.Categorical(data).astype(dtype) is only of that type in the second case (when dtype is datetime64[ns, MET]).

In the first case (when dtype is datetime64[ns]), it is of type np.array.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm well I think that is a problem that as type would return a numpy array for "datetime64[ns]" and a DatetimeArray for "datetime64[ns, ]" but @jbrockmendel or @TomAugspurger might know more

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I don't totally follow the discussion but it seems like #28448 is about Categorical.astype(datetime64[ns, tz]), which return a DatatimeArray.

So I think the test should be something like

result = pd.Categorical(data).astype(dtype)
tm.assert_equal(result, expected)

no asarray.

Copy link
Member

Choose a reason for hiding this comment

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

Right I think so too. But the problem is Categorical.astype("datetime64[ns]") returns a NumPy array, so tz-naive date times would be a numpy array where tz-aware would be a DTA.

That seems odd to me but I don't know the entire history of these

Copy link
Contributor

Choose a reason for hiding this comment

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

In theory Categorical.astype('datetime64[ns]') could return a tz-naive DatetimeArray. I'm not sure if it was discussed explicitly when DatetimeArray was added, but changing that would be API-breaking and IMO not worth it.

Copy link
Member

Choose a reason for hiding this comment

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

@WillAyd I tend to agree with everyone above in that this is sub-optimal behavior but not worth changing on its own*. Is this a problem or A Problem?

* There's been some discussion of making EA.astype always return EA at some point, which I think would include this.

Copy link
Member

Choose a reason for hiding this comment

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

If you guys don't think so then no. I just wasn't aware of the historical context hence the ping - thanks for the insights.

So @MarcoGorelli still should update the tests to use asarray but vary expectation accordingly

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, thanks all for your feedback - I've updated the tests

@WillAyd WillAyd added Categorical Categorical Data Type Testing pandas testing functions or related to the test suite labels Oct 29, 2019
"data, dtype, expected",
[
(
"2015-01-01",
Copy link
Member

Choose a reason for hiding this comment

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

Side note - since data is always the same no need to parametrize

… types (datetimearray and np.array) are used
Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

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

lgtm @TomAugspurger or @jbrockmendel feel free to merge if it looks good to you as well

@WillAyd WillAyd added this to the 1.0 milestone Oct 30, 2019
@TomAugspurger
Copy link
Contributor

TomAugspurger commented Oct 30, 2019 via email

(
"datetime64[ns, MET]",
pd.arrays.DatetimeArray(
pd.array([pd.Timestamp("2015-01-01 00:00:00+0100", tz="MET")])
Copy link
Member

Choose a reason for hiding this comment

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

pd.DatetimeIndex([pd.Timestamp("2015-01-01 00:00:00+0100", tz="MET")]).array would be more succint. would it be any less clear?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's nicer, thanks for the suggestion - I've made the change

@jbrockmendel
Copy link
Member

small comment, not a blocker. lgtm

@jreback jreback merged commit b501aa0 into pandas-dev:master Oct 31, 2019
@jreback
Copy link
Contributor

jreback commented Oct 31, 2019

thanks @MarcoGorelli

@MarcoGorelli MarcoGorelli deleted the add-test-for-28448 branch October 31, 2019 16:03
Reksbril pushed a commit to Reksbril/pandas that referenced this pull request Nov 18, 2019
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Categorical Categorical Data Type Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't convert Categorical to 'datetime64[ns, MET]', but can do it with Series
5 participants