Skip to content

TST: Fix test for datetime categorical #10501

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 1 commit into from
Jul 6, 2015

Conversation

sinhrks
Copy link
Member

@sinhrks sinhrks commented Jul 3, 2015

Related to #10465, but different part.

Current test_groupby_datetime_categorical in test_groupby.py is incorrect, the actual result returns CategoricalIndex as level 0, otherwise expected result uses DatetimeIndex as level 0. Changed to use the same dtype and added explicit comparison.

Actual Result (current test case)

levels = pd.date_range('2014-01-01', periods=4)
codes = np.random.randint(0, 4, size=100)
cats = pd.Categorical.from_codes(codes, levels, name='myfactor', ordered=True)
data = pd.DataFrame(np.random.randn(100, 4))
grouped = data.groupby(cats)
desc_result = grouped.describe()
desc_result.index.get_level_values(0)
# CategoricalIndex([2014-01-01T09:00:00.000000000+0900,
#                  ...
#                   2014-01-04T09:00:00.000000000+0900],
#                  categories=[2014-01-01 00:00:00, 2014-01-02 00:00:00, 2014-01-03 00:00:00, 2014-01-04 00:00:00],
# ordered=True, name=u'myfactor', dtype='category')

Expected Result (current test case)

It must be CategoricalIndex.

idx = cats.codes.argsort()
ord_labels = np.asarray(cats).take(idx)
ord_data = data.take(idx)
expected = ord_data.groupby(ord_labels, sort=False).describe()
expected.index.names = ['myfactor', None]
expected.index.get_level_values(0)
# DatetimeIndex(['2014-01-01', '2014-01-01', '2014-01-01', '2014-01-01',
#               ...
#               '2014-01-04', '2014-01-04', '2014-01-04', '2014-01-04'],
#               dtype='datetime64[ns]', name=u'myfactor', freq=None, tz=None)

@sinhrks sinhrks added Testing pandas testing functions or related to the test suite Categorical Categorical Data Type labels Jul 3, 2015
@sinhrks sinhrks added this to the 0.17.0 milestone Jul 3, 2015
@jreback
Copy link
Contributor

jreback commented Jul 6, 2015

lgtm, merge when ready

@sinhrks
Copy link
Member Author

sinhrks commented Jul 6, 2015

Thanks, merging.

sinhrks added a commit that referenced this pull request Jul 6, 2015
TST: Fix test for datetime categorical
@sinhrks sinhrks merged commit 9fef291 into pandas-dev:master Jul 6, 2015
@sinhrks sinhrks deleted the test_dtcategorical branch July 6, 2015 12:59
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.

2 participants