Skip to content

Commit 4962131

Browse files
gfyoungjreback
authored andcommitted
MAINT: Removed some warnings in tests
Per discussion with @jreback <a href="https://github.com/pydata/pandas /pull/13671#issuecomment-233508468">here</a>. Author: gfyoung <[email protected]> Closes #13702 from gfyoung/test-warnings-remove and squashes the following commits: e7292d3 [gfyoung] MAINT: Removed some warnings in tests
1 parent 016b352 commit 4962131

File tree

2 files changed

+32
-51
lines changed

2 files changed

+32
-51
lines changed

pandas/core/internals.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1490,7 +1490,7 @@ def putmask(self, mask, new, align=True, inplace=False, axis=0,
14901490
if isinstance(new, np.ndarray) and len(new) == len(mask):
14911491
new = new[mask]
14921492

1493-
mask = mask.reshape(new_values.shape)
1493+
mask = _safe_reshape(mask, new_values.shape)
14941494
new_values[mask] = new
14951495
new_values = self._try_coerce_result(new_values)
14961496
return [self.make_block(values=new_values)]

pandas/tests/test_categorical.py

+31-50
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# pylint: disable=E1101,E1103,W0232
33

4-
import os
54
import sys
65
from datetime import datetime
76
from distutils.version import LooseVersion
@@ -2906,54 +2905,41 @@ def test_value_counts(self):
29062905
tm.assert_series_equal(res, exp)
29072906

29082907
def test_value_counts_with_nan(self):
2909-
# https://github.com/pydata/pandas/issues/9443
2908+
# see gh-9443
29102909

2910+
# sanity check
29112911
s = pd.Series(["a", "b", "a"], dtype="category")
2912-
tm.assert_series_equal(
2913-
s.value_counts(dropna=True),
2914-
pd.Series([2, 1], index=pd.CategoricalIndex(["a", "b"])))
2915-
tm.assert_series_equal(
2916-
s.value_counts(dropna=False),
2917-
pd.Series([2, 1], index=pd.CategoricalIndex(["a", "b"])))
2912+
exp = pd.Series([2, 1], index=pd.CategoricalIndex(["a", "b"]))
29182913

2919-
s = pd.Series(["a", "b", None, "a", None, None], dtype="category")
2920-
tm.assert_series_equal(
2921-
s.value_counts(dropna=True),
2922-
pd.Series([2, 1], index=pd.CategoricalIndex(["a", "b"])))
2923-
tm.assert_series_equal(
2924-
s.value_counts(dropna=False),
2925-
pd.Series([3, 2, 1], index=pd.CategoricalIndex([np.nan, "a", "b"])))
2926-
# When we aren't sorting by counts, and np.nan isn't a
2927-
# category, it should be last.
2928-
tm.assert_series_equal(
2929-
s.value_counts(dropna=False, sort=False),
2930-
pd.Series([2, 1, 3],
2931-
index=pd.CategoricalIndex(["a", "b", np.nan])))
2914+
res = s.value_counts(dropna=True)
2915+
tm.assert_series_equal(res, exp)
29322916

2933-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
2934-
s = pd.Series(pd.Categorical(["a", "b", "a"],
2935-
categories=["a", "b", np.nan]))
2917+
res = s.value_counts(dropna=True)
2918+
tm.assert_series_equal(res, exp)
29362919

2937-
# internal categories are different because of NaN
2938-
exp = pd.Series([2, 1], index=pd.CategoricalIndex(["a", "b"]))
2939-
tm.assert_series_equal(s.value_counts(dropna=True), exp,
2940-
check_categorical=False)
2941-
exp = pd.Series([2, 1, 0],
2942-
index=pd.CategoricalIndex(["a", "b", np.nan]))
2943-
tm.assert_series_equal(s.value_counts(dropna=False), exp,
2944-
check_categorical=False)
2920+
# same Series via two different constructions --> same behaviour
2921+
series = [
2922+
pd.Series(["a", "b", None, "a", None, None], dtype="category"),
2923+
pd.Series(pd.Categorical(["a", "b", None, "a", None, None],
2924+
categories=["a", "b"]))
2925+
]
29452926

2946-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
2947-
s = pd.Series(pd.Categorical(["a", "b", None, "a", None, None],
2948-
categories=["a", "b", np.nan]))
2927+
for s in series:
2928+
# None is a NaN value, so we exclude its count here
2929+
exp = pd.Series([2, 1], index=pd.CategoricalIndex(["a", "b"]))
2930+
res = s.value_counts(dropna=True)
2931+
tm.assert_series_equal(res, exp)
29492932

2950-
exp = pd.Series([2, 1], index=pd.CategoricalIndex(["a", "b"]))
2951-
tm.assert_series_equal(s.value_counts(dropna=True), exp,
2952-
check_categorical=False)
2953-
exp = pd.Series([3, 2, 1],
2954-
index=pd.CategoricalIndex([np.nan, "a", "b"]))
2955-
tm.assert_series_equal(s.value_counts(dropna=False), exp,
2956-
check_categorical=False)
2933+
# we don't exclude the count of None and sort by counts
2934+
exp = pd.Series([3, 2, 1], index=pd.CategoricalIndex([np.nan, "a", "b"]))
2935+
res = s.value_counts(dropna=False)
2936+
tm.assert_series_equal(res, exp)
2937+
2938+
# When we aren't sorting by counts, and np.nan isn't a
2939+
# category, it should be last.
2940+
exp = pd.Series([2, 1, 3], index=pd.CategoricalIndex(["a", "b", np.nan]))
2941+
res = s.value_counts(dropna=False, sort=False)
2942+
tm.assert_series_equal(res, exp)
29572943

29582944
def test_groupby(self):
29592945

@@ -4113,16 +4099,11 @@ def f():
41134099
res = df.dropna()
41144100
tm.assert_frame_equal(res, df_exp_drop_all)
41154101

4116-
# make sure that fillna takes both missing values and NA categories
4117-
# into account
4118-
c = Categorical(["a", "b", np.nan])
4119-
with tm.assert_produces_warning(FutureWarning):
4120-
c.set_categories(["a", "b", np.nan], rename=True, inplace=True)
4121-
4122-
c[0] = np.nan
4102+
# make sure that fillna takes missing values into account
4103+
c = Categorical([np.nan, "b", np.nan], categories=["a", "b"])
41234104
df = pd.DataFrame({"cats": c, "vals": [1, 2, 3]})
41244105

4125-
cat_exp = Categorical(["a", "b", "a"], categories=["a", "b", np.nan])
4106+
cat_exp = Categorical(["a", "b", "a"], categories=["a", "b"])
41264107
df_exp = pd.DataFrame({"cats": cat_exp, "vals": [1, 2, 3]})
41274108

41284109
res = df.fillna("a")

0 commit comments

Comments
 (0)