Skip to content

Commit

Permalink
Deprecate Series.valid
Browse files Browse the repository at this point in the history
  • Loading branch information
tp committed Dec 15, 2017
1 parent 0303d0d commit 537550e
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 26 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.22.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ Deprecations
- ``Series.from_array`` and ``SparseSeries.from_array`` are deprecated. Use the normal constructor ``Series(..)`` and ``SparseSeries(..)`` instead (:issue:`18213`).
- ``DataFrame.as_matrix`` is deprecated. Use ``DataFrame.values`` instead (:issue:`18458`).
- ``Series.asobject``, ``DatetimeIndex.asobject``, ``PeriodIndex.asobject`` and ``TimeDeltaIndex.asobject`` have been deprecated. Use ``.astype(object)`` instead (:issue:`18572`)
- ``Series.valid`` is deprecated. Use ``Series.dropna`` instead (:issue:`18800`).

.. _whatsnew_0220.prior_deprecations:

Expand Down
11 changes: 8 additions & 3 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
_accessors = frozenset(['dt', 'cat', 'str'])
_deprecations = generic.NDFrame._deprecations | frozenset(
['asobject', 'sortlevel', 'reshape', 'get_value', 'set_value',
'from_csv'])
'from_csv', 'valid'])
_allow_index_ops = True

def __init__(self, data=None, index=None, dtype=None, name=None,
Expand Down Expand Up @@ -3006,8 +3006,13 @@ def dropna(self, axis=0, inplace=False, **kwargs):
else:
return self.copy()

valid = lambda self, inplace=False, **kwargs: self.dropna(inplace=inplace,
**kwargs)
def valid(self, inplace=False, **kwargs):
"""DEPRECATED. Series.valid will be removed in a future version.
Use :meth:`Series.dropna` instead.
"""
warnings.warn("Method .valid will be removed in a future version. "
"Use .dropna instead.", FutureWarning, stacklevel=2)
return self.dropna(inplace=inplace, **kwargs)

@Appender(generic._shared_docs['valid_index'] % {
'position': 'first', 'klass': 'Series'})
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/sparse/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ def dropna(self, axis=0, inplace=False, **kwargs):
"""
# TODO: make more efficient
axis = self._get_axis_number(axis or 0)
dense_valid = self.to_dense().valid()
dense_valid = self.to_dense().dropna()
if inplace:
raise NotImplementedError("Cannot perform inplace dropna"
" operations on a SparseSeries")
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/frame/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,10 +766,10 @@ def test_combineFrame(self):

added = self.frame + frame_copy

indexer = added['A'].valid().index
indexer = added['A'].dropna().index
exp = (self.frame['A'] * 2).copy()

tm.assert_series_equal(added['A'].valid(), exp.loc[indexer])
tm.assert_series_equal(added['A'].dropna(), exp.loc[indexer])

exp.loc[~exp.index.isin(indexer)] = np.nan
tm.assert_series_equal(added['A'], exp.loc[added['A'].index])
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def test_shift(self):
unshifted = shifted.shift(-1)
tm.assert_index_equal(shifted.index, ps.index)
tm.assert_index_equal(unshifted.index, ps.index)
tm.assert_numpy_array_equal(unshifted.iloc[:, 0].valid().values,
tm.assert_numpy_array_equal(unshifted.iloc[:, 0].dropna().values,
ps.iloc[:-1, 0].values)

shifted2 = ps.shift(1, 'B')
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/generic/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,10 @@ def test_validate_bool_args(self):
with pytest.raises(ValueError):
super(DataFrame, df).mask(cond=df.a > 2, inplace=value)

def test_valid_deprecated(self):
with tm.assert_produces_warning(FutureWarning):
pd.Series([]).valid()

def test_copy_and_deepcopy(self):
# GH 15444
for shape in [0, 1, 2]:
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def test_cummin(self):
ts = self.ts.copy()
ts[::2] = np.NaN
result = ts.cummin()[1::2]
expected = np.minimum.accumulate(ts.valid())
expected = np.minimum.accumulate(ts.dropna())

tm.assert_series_equal(result, expected)

Expand All @@ -406,7 +406,7 @@ def test_cummax(self):
ts = self.ts.copy()
ts[::2] = np.NaN
result = ts.cummax()[1::2]
expected = np.maximum.accumulate(ts.valid())
expected = np.maximum.accumulate(ts.dropna())

tm.assert_series_equal(result, expected)

Expand Down Expand Up @@ -570,7 +570,7 @@ def _check_accum_op(self, name, check_dtype=True):
ts[::2] = np.NaN

result = func(ts)[1::2]
expected = func(np.array(ts.valid()))
expected = func(np.array(ts.dropna()))

tm.assert_numpy_array_equal(result.values, expected,
check_dtype=False)
Expand Down Expand Up @@ -1530,7 +1530,7 @@ def test_shift_categorical(self):
# GH 9416
s = pd.Series(['a', 'b', 'c', 'd'], dtype='category')

assert_series_equal(s.iloc[:-1], s.shift(1).shift(-1).valid())
assert_series_equal(s.iloc[:-1], s.shift(1).shift(-1).dropna())

sp1 = s.shift(1)
assert_index_equal(s.index, sp1.index)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ def test_valid(self):
ts = self.ts.copy()
ts[::2] = np.NaN

result = ts.valid()
result = ts.dropna()
assert len(result) == ts.count()
tm.assert_series_equal(result, ts[1::2])
tm.assert_series_equal(result, ts[pd.notna(ts)])
Expand Down
14 changes: 7 additions & 7 deletions pandas/tests/series/test_quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class TestSeriesQuantile(TestData):
def test_quantile(self):

q = self.ts.quantile(0.1)
assert q == np.percentile(self.ts.valid(), 10)
assert q == np.percentile(self.ts.dropna(), 10)

q = self.ts.quantile(0.9)
assert q == np.percentile(self.ts.valid(), 90)
assert q == np.percentile(self.ts.dropna(), 90)

# object dtype
q = Series(self.ts, dtype=object).quantile(0.9)
assert q == np.percentile(self.ts.valid(), 90)
assert q == np.percentile(self.ts.dropna(), 90)

# datetime64[ns] dtype
dts = self.ts.index.to_series()
Expand All @@ -49,8 +49,8 @@ def test_quantile_multi(self):

qs = [.1, .9]
result = self.ts.quantile(qs)
expected = pd.Series([np.percentile(self.ts.valid(), 10),
np.percentile(self.ts.valid(), 90)],
expected = pd.Series([np.percentile(self.ts.dropna(), 10),
np.percentile(self.ts.dropna(), 90)],
index=qs, name=self.ts.name)
tm.assert_series_equal(result, expected)

Expand All @@ -72,9 +72,9 @@ def test_quantile_interpolation(self):

# interpolation = linear (default case)
q = self.ts.quantile(0.1, interpolation='linear')
assert q == np.percentile(self.ts.valid(), 10)
assert q == np.percentile(self.ts.dropna(), 10)
q1 = self.ts.quantile(0.1)
assert q1 == np.percentile(self.ts.valid(), 10)
assert q1 == np.percentile(self.ts.dropna(), 10)

# test with and without interpolation keyword
assert q == q1
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/series/test_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ def test_sort_values(self):

# ascending=False
ordered = ts.sort_values(ascending=False)
expected = np.sort(ts.valid().values)[::-1]
assert_almost_equal(expected, ordered.valid().values)
expected = np.sort(ts.dropna().values)[::-1]
assert_almost_equal(expected, ordered.dropna().values)
ordered = ts.sort_values(ascending=False, na_position='first')
assert_almost_equal(expected, ordered.valid().values)
assert_almost_equal(expected, ordered.dropna().values)

# ascending=[False] should behave the same as ascending=False
ordered = ts.sort_values(ascending=[False])
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/series/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_shift(self):

tm.assert_index_equal(shifted.index, self.ts.index)
tm.assert_index_equal(unshifted.index, self.ts.index)
tm.assert_numpy_array_equal(unshifted.valid().values,
tm.assert_numpy_array_equal(unshifted.dropna().values,
self.ts.values[:-1])

offset = BDay()
Expand All @@ -69,7 +69,7 @@ def test_shift(self):
unshifted = shifted.shift(-1)
tm.assert_index_equal(shifted.index, ps.index)
tm.assert_index_equal(unshifted.index, ps.index)
tm.assert_numpy_array_equal(unshifted.valid().values, ps.values[:-1])
tm.assert_numpy_array_equal(unshifted.dropna().values, ps.values[:-1])

shifted2 = ps.shift(1, 'B')
shifted3 = ps.shift(1, BDay())
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/sparse/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,9 +796,9 @@ def _compare_all(obj):
def test_dropna(self):
sp = SparseSeries([0, 0, 0, nan, nan, 5, 6], fill_value=0)

sp_valid = sp.valid()
sp_valid = sp.dropna()

expected = sp.to_dense().valid()
expected = sp.to_dense().dropna()
expected = expected[expected != 0]
exp_arr = pd.SparseArray(expected.values, fill_value=0, kind='block')
tm.assert_sp_array_equal(sp_valid.values, exp_arr)
Expand Down

0 comments on commit 537550e

Please sign in to comment.