Skip to content

Commit b1ac2d4

Browse files
committed
move tests and add whatsnew
1 parent a6d3700 commit b1ac2d4

File tree

4 files changed

+125
-3
lines changed

4 files changed

+125
-3
lines changed

doc/source/whatsnew/v0.22.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Other Enhancements
2424

2525
- Better support for :func:`Dataframe.style.to_excel` output with the ``xlsxwriter`` engine. (:issue:`16149`)
2626
- :func:`pandas.tseries.frequencies.to_offset` now accepts leading '+' signs e.g. '+1h'. (:issue:`18171`)
27-
-
27+
- :func:`Series.fillna` now accepts a Series or a dict as a ``value`` (:issue:`17033`)
2828

2929
.. _whatsnew_0220.api_breaking:
3030

pandas/tests/frame/test_missing.py

+75-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from pandas.compat import lrange
1212
from pandas import (DataFrame, Series, Timestamp,
13-
date_range)
13+
date_range, Categorical)
1414
import pandas as pd
1515

1616
from pandas.util.testing import assert_series_equal, assert_frame_equal
@@ -270,6 +270,80 @@ def test_fillna(self):
270270
pd.Timestamp('2012-11-11 00:00:00+01:00')]})
271271
assert_frame_equal(df.fillna(method='bfill'), exp)
272272

273+
def test_na_actions(self):
274+
275+
cat = pd.Categorical([1, 2, 3, np.nan], categories=[1, 2, 3])
276+
vals = ["a", "b", np.nan, "d"]
277+
df = pd.DataFrame({"cats": cat, "vals": vals})
278+
cat2 = pd.Categorical([1, 2, 3, 3], categories=[1, 2, 3])
279+
vals2 = ["a", "b", "b", "d"]
280+
df_exp_fill = pd.DataFrame({"cats": cat2, "vals": vals2})
281+
cat3 = pd.Categorical([1, 2, 3], categories=[1, 2, 3])
282+
vals3 = ["a", "b", np.nan]
283+
df_exp_drop_cats = pd.DataFrame({"cats": cat3, "vals": vals3})
284+
cat4 = pd.Categorical([1, 2], categories=[1, 2, 3])
285+
vals4 = ["a", "b"]
286+
df_exp_drop_all = pd.DataFrame({"cats": cat4, "vals": vals4})
287+
288+
# fillna
289+
res = df.fillna(value={"cats": 3, "vals": "b"})
290+
tm.assert_frame_equal(res, df_exp_fill)
291+
292+
def f():
293+
df.fillna(value={"cats": 4, "vals": "c"})
294+
295+
pytest.raises(ValueError, f)
296+
297+
res = df.fillna(method='pad')
298+
tm.assert_frame_equal(res, df_exp_fill)
299+
300+
res = df.dropna(subset=["cats"])
301+
tm.assert_frame_equal(res, df_exp_drop_cats)
302+
303+
res = df.dropna()
304+
tm.assert_frame_equal(res, df_exp_drop_all)
305+
306+
# make sure that fillna takes missing values into account
307+
c = Categorical([np.nan, "b", np.nan], categories=["a", "b"])
308+
df = pd.DataFrame({"cats": c, "vals": [1, 2, 3]})
309+
310+
cat_exp = Categorical(["a", "b", "a"], categories=["a", "b"])
311+
df_exp = pd.DataFrame({"cats": cat_exp, "vals": [1, 2, 3]})
312+
313+
res = df.fillna("a")
314+
tm.assert_frame_equal(res, df_exp)
315+
316+
# GH 14021
317+
# np.nan should always be a is a valid filler
318+
cat = Categorical([np.nan, 2, np.nan])
319+
val = Categorical([np.nan, np.nan, np.nan])
320+
df = DataFrame({"cats": cat, "vals": val})
321+
res = df.fillna(df.median())
322+
v_exp = [np.nan, np.nan, np.nan]
323+
df_exp = pd.DataFrame({"cats": [2, 2, 2], "vals": v_exp},
324+
dtype='category')
325+
tm.assert_frame_equal(res, df_exp)
326+
327+
result = df.cats.fillna(np.nan)
328+
tm.assert_series_equal(result, df.cats)
329+
result = df.vals.fillna(np.nan)
330+
tm.assert_series_equal(result, df.vals)
331+
332+
idx = pd.DatetimeIndex(['2011-01-01 09:00', '2016-01-01 23:45',
333+
'2011-01-01 09:00', pd.NaT, pd.NaT])
334+
df = DataFrame({'a': pd.Categorical(idx)})
335+
tm.assert_frame_equal(df.fillna(value=pd.NaT), df)
336+
337+
idx = pd.PeriodIndex(['2011-01', '2011-01', '2011-01',
338+
pd.NaT, pd.NaT], freq='M')
339+
df = DataFrame({'a': pd.Categorical(idx)})
340+
tm.assert_frame_equal(df.fillna(value=pd.NaT), df)
341+
342+
idx = pd.TimedeltaIndex(['1 days', '2 days',
343+
'1 days', pd.NaT, pd.NaT])
344+
df = pd.DataFrame({'a': pd.Categorical(idx)})
345+
tm.assert_frame_equal(df.fillna(value=pd.NaT), df)
346+
273347
def test_fillna_downcast(self):
274348
# GH 15277
275349
# infer int64 from float64

pandas/tests/series/test_missing.py

+46-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
import pandas as pd
1313

1414
from pandas import (Series, DataFrame, isna, date_range,
15-
MultiIndex, Index, Timestamp, NaT, IntervalIndex)
15+
MultiIndex, Index, Timestamp, NaT, IntervalIndex,
16+
Categorical)
1617
from pandas.compat import range
1718
from pandas._libs.tslib import iNaT
1819
from pandas.core.series import remove_na
@@ -363,6 +364,50 @@ def test_fillna_raise(self):
363364
with pytest.raises(ValueError):
364365
s.fillna(1, limit=limit, method=method)
365366

367+
@pytest.mark.parametrize('fill_value, expected_output', [
368+
('a', ['a', 'a', 'b', 'a', 'a']),
369+
({1: 'a', 3: 'b', 4: 'b'}, ['a', 'a', 'b', 'b', 'b']),
370+
({1: 'a'}, ['a', 'a', 'b', np.nan, np.nan]),
371+
({1: 'a', 3: 'b'}, ['a', 'a', 'b', 'b', np.nan]),
372+
(Series('a'), ['a', np.nan, 'b', np.nan, np.nan]),
373+
(Series('a', index=[1]), ['a', 'a', 'b', np.nan, np.nan]),
374+
(Series({1: 'a', 3: 'b'}), ['a', 'a', 'b', 'b', np.nan]),
375+
(Series(['a', 'b'], index=[3, 4]), ['a', np.nan, 'b', 'a', 'b'])
376+
])
377+
def test_fillna_categorical(self, fill_value, expected_output):
378+
# GH 17033
379+
# Test fillna for a Categorical series
380+
data = ['a', np.nan, 'b', np.nan, np.nan]
381+
s = Series(Categorical(data, categories=['a', 'b']))
382+
exp = Series(Categorical(expected_output, categories=['a', 'b']))
383+
tm.assert_series_equal(s.fillna(fill_value), exp)
384+
385+
def test_fillna_categorical_raise(self):
386+
data = ['a', np.nan, 'b', np.nan, np.nan]
387+
s = Series(Categorical(data, categories=['a', 'b']))
388+
389+
with tm.assert_raises_regex(ValueError,
390+
"fill value must be in categories"):
391+
s.fillna('d')
392+
393+
with tm.assert_raises_regex(ValueError,
394+
"fill value must be in categories"):
395+
s.fillna(Series('d'))
396+
397+
with tm.assert_raises_regex(ValueError,
398+
"fill value must be in categories"):
399+
s.fillna({1: 'd', 3: 'a'})
400+
401+
with tm.assert_raises_regex(TypeError,
402+
'"value" parameter must be a scalar or '
403+
'dict, but you passed a "list"'):
404+
s.fillna(['a', 'b'])
405+
406+
with tm.assert_raises_regex(TypeError,
407+
'"value" parameter must be a scalar or '
408+
'dict, but you passed a "tuple"'):
409+
s.fillna(('a', 'b'))
410+
366411
def test_fillna_nat(self):
367412
series = Series([0, 1, 2, iNaT], dtype='M8[ns]')
368413

pandas/tests/test_categorical.py

+3
Original file line numberDiff line numberDiff line change
@@ -4496,6 +4496,7 @@ def test_numpy_reshape(self):
44964496
tm.assert_raises_regex(ValueError, msg, np.reshape,
44974497
cat, cat.shape, order='F')
44984498

4499+
<<<<<<< HEAD
44994500
def test_na_actions(self):
45004501

45014502
cat = Categorical([1, 2, 3, np.nan], categories=[1, 2, 3])
@@ -4608,6 +4609,8 @@ def test_fillna_series_categorical_errormsg(self):
46084609
'dict, but you passed a "list"'):
46094610
s.fillna(['a', 'b'])
46104611

4612+
=======
4613+
>>>>>>> move tests and add whatsnew
46114614
def test_astype_to_other(self):
46124615

46134616
s = self.cat['value_group']

0 commit comments

Comments
 (0)