Skip to content

Commit ca3cb74

Browse files
committed
EHN: Implement method argument for DataFrame.replace
1 parent 0bfb61b commit ca3cb74

File tree

125 files changed

+40
-6
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+40
-6
lines changed
88.8 KB

doc/source/savefig/andrews_curves.png

99.7 KB
39.3 KB
59.9 KB
39 KB

doc/source/savefig/bar_plot_ex.png

5.56 KB
10.2 KB
9.19 KB
9.13 KB

doc/source/savefig/basics_assign.png

16.2 KB

doc/source/savefig/bollinger.png

41.2 KB

doc/source/savefig/bootstrap_plot.png

32 KB
9.71 KB

doc/source/savefig/box_new_kwargs.png

9.11 KB

doc/source/savefig/box_plot_ex.png

11.4 KB

doc/source/savefig/box_plot_ex2.png

14.6 KB

doc/source/savefig/box_plot_ex3.png

18.1 KB

doc/source/savefig/box_plot_new.png

10.1 KB
13.5 KB
21.5 KB

doc/source/savefig/cubehelix.png

103 KB

doc/source/savefig/cubehelix_cm.png

103 KB

doc/source/savefig/df_pie_plot.png

22.5 KB

doc/source/savefig/df_plot_xy.png

28.7 KB
10.8 KB

doc/source/savefig/ewma_ex.png

33.1 KB
33.7 KB

doc/source/savefig/frame_hist_ex.png

13.5 KB
74.7 KB
59.4 KB
77.1 KB
55.3 KB

doc/source/savefig/greens.png

11 KB
9.83 KB
9.83 KB
46.3 KB

doc/source/savefig/grouped_hist.png

10.5 KB

doc/source/savefig/hexbin_plot.png

59.3 KB
72 KB

doc/source/savefig/hist_new.png

11.4 KB
8.72 KB
13 KB

doc/source/savefig/hist_plot_ex.png

8.47 KB

doc/source/savefig/kde_plot.png

21.4 KB

doc/source/savefig/lag_plot.png

31 KB
44 KB
47.6 KB
50 KB
7.83 KB
12.5 KB
17.6 KB
21.8 KB
16.5 KB
6.26 KB
16.3 KB
21.8 KB
26.4 KB
23.2 KB
26.4 KB

doc/source/savefig/merging_join.png

6.54 KB
6.21 KB
9.52 KB
6.61 KB
11.4 KB
7.43 KB
6.21 KB
7.43 KB
9.52 KB
8.65 KB
6.83 KB
9.87 KB
5.02 KB

doc/source/savefig/merging_update.png

6.2 KB
138 KB
127 KB
23.1 KB

doc/source/savefig/radviz.png

22.7 KB
27 KB
44.3 KB
35.3 KB
38.7 KB
194 KB

doc/source/savefig/scatter_plot.png

12.4 KB
27.9 KB
23.3 KB
19 KB
28.6 KB
43.4 KB
30.5 KB
22.8 KB
16.4 KB
14.6 KB
29.2 KB
31.6 KB
30.7 KB
51.1 KB

doc/source/whatsnew/v0.23.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ Other Enhancements
338338
- For subclassed ``DataFrames``, :func:`DataFrame.apply` will now preserve the ``Series`` subclass (if defined) when passing the data to the applied function (:issue:`19822`)
339339
- :func:`DataFrame.from_dict` now accepts a ``columns`` argument that can be used to specify the column names when ``orient='index'`` is used (:issue:`18529`)
340340
- Added option ``display.html.use_mathjax`` so `MathJax <https://www.mathjax.org/>`_ can be disabled when rendering tables in ``Jupyter`` notebooks (:issue:`19856`, :issue:`19824`)
341-
341+
- :func:`DataFrame.replace` now supports the ``method`` parameter which can be used to specify the replacement method when ``to_replace`` is a scalar, list or tuple and ``value`` is None (:issue:`19632`)
342342

343343
.. _whatsnew_0230.api_breaking:
344344

doc/test.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"schema": {"fields":[{"name":"index","type":"integer"},{"name":"bar","type":"string"},{"name":"baz","type":"datetime"},{"name":"foo","type":"integer"},{"name":"qux","type":"any","constraints":{"enum":["a","b","c"]},"ordered":false}],"primaryKey":["index"],"pandas_version":"0.20.0"}, "data": [{"index":0,"bar":"a","baz":"2018-01-01T00:00:00.000Z","foo":1,"qux":"a"},{"index":1,"bar":"b","baz":"2018-01-02T00:00:00.000Z","foo":2,"qux":"b"},{"index":2,"bar":"c","baz":"2018-01-03T00:00:00.000Z","foo":3,"qux":"c"},{"index":3,"bar":"d","baz":"2018-01-04T00:00:00.000Z","foo":4,"qux":"c"}]}

pandas/core/generic.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -4887,7 +4887,7 @@ def bfill(self, axis=None, inplace=False, limit=None, downcast=None):
48874887
``to_replace`` must be ``None``.
48884888
method : string, optional, {'pad', 'ffill', 'bfill'}
48894889
The method to use when for replacement, when ``to_replace`` is a
4890-
``list``.
4890+
scalar, list or tuple and ``value`` is None.
48914891
48924892
See Also
48934893
--------
@@ -5056,6 +5056,10 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
50565056
to_replace = [to_replace]
50575057

50585058
if isinstance(to_replace, (tuple, list)):
5059+
if isinstance(self, pd.DataFrame):
5060+
return self.apply(_single_replace,
5061+
args=(to_replace, method, inplace,
5062+
limit))
50595063
return _single_replace(self, to_replace, method, inplace,
50605064
limit)
50615065

pandas/tests/frame/test_replace.py

+33-4
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ def test_replace_inplace(self):
3333
tsframe.replace(nan, 0, inplace=True)
3434
assert_frame_equal(tsframe, self.tsframe.fillna(0))
3535

36-
pytest.raises(TypeError, self.tsframe.replace, nan, inplace=True)
37-
pytest.raises(TypeError, self.tsframe.replace, nan)
38-
3936
# mixed type
4037
mf = self.mixed_frame
4138
mf.iloc[5:20, mf.columns.get_loc('foo')] = nan
@@ -720,7 +717,6 @@ def test_replace_simple_nested_dict_with_nonexistent_value(self):
720717
assert_frame_equal(expected, result)
721718

722719
def test_replace_value_is_none(self):
723-
pytest.raises(TypeError, self.tsframe.replace, nan)
724720
orig_value = self.tsframe.iloc[0, 0]
725721
orig2 = self.tsframe.iloc[1, 0]
726722

@@ -1072,3 +1068,36 @@ def test_replace_with_empty_dictlike(self):
10721068

10731069
assert_frame_equal(df, df.replace({'b': {}}))
10741070
assert_frame_equal(df, df.replace(Series({'b': {}})))
1071+
1072+
@pytest.mark.parametrize("to_replace, method, expected", [
1073+
(0, 'bfill', {'A': [1, 1, 2],
1074+
'B': [5, nan, 7],
1075+
'C': ['a', 'b', 'c']}),
1076+
(nan, 'bfill', {'A': [0, 1, 2],
1077+
'B': [5.0, 7.0, 7.0],
1078+
'C': ['a', 'b', 'c']}),
1079+
('d', 'ffill', {'A': [0, 1, 2],
1080+
'B': [5, nan, 7],
1081+
'C': ['a', 'b', 'c']}),
1082+
([0, 2], 'bfill', {'A': [1, 1, 2],
1083+
'B': [5, nan, 7],
1084+
'C': ['a', 'b', 'c']}),
1085+
([1, 2], 'pad', {'A': [0, 0, 0],
1086+
'B': [5, nan, 7],
1087+
'C': ['a', 'b', 'c']}),
1088+
((1, 2), 'bfill', {'A': [0, 2, 2],
1089+
'B': [5, nan, 7],
1090+
'C': ['a', 'b', 'c']}),
1091+
(['b', 'c'], 'ffill', {'A': [0, 1, 2],
1092+
'B': [5, nan, 7],
1093+
'C': ['a', 'a', 'a']}),
1094+
])
1095+
def test_replace_method(self, to_replace, method, expected):
1096+
# GH 19632
1097+
df = DataFrame({'A': [0, 1, 2],
1098+
'B': [5, nan, 7],
1099+
'C': ['a', 'b', 'c']})
1100+
1101+
result = df.replace(to_replace=to_replace, value=None, method=method)
1102+
expected = DataFrame(expected)
1103+
assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)