Skip to content

PERF: Series.dropna with non-nan dtype blocks #11159

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
Oct 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions asv_bench/benchmarks/series_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,23 @@ def setup(self):
def time_series_nsmallest2(self):
self.s2.nsmallest(3, take_last=True)
self.s2.nsmallest(3, take_last=False)


class series_dropna_int64(object):
goal_time = 0.2

def setup(self):
self.s = Series(np.random.randint(1, 10, 1000000))

def time_series_dropna_int64(self):
self.s.dropna()

class series_dropna_datetime(object):
goal_time = 0.2

def setup(self):
self.s = Series(pd.date_range('2000-01-01', freq='S', periods=1000000))
self.s[np.random.randint(1, 1000000, 100)] = pd.NaT

def time_series_dropna_datetime(self):
self.s.dropna()
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.17.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Performance Improvements
~~~~~~~~~~~~~~~~~~~~~~~~

- Checking monotonic-ness before sorting on an index (:issue:`11080`)
- ``Series.dropna`` performance improvement when its dtype can't contain ``NaN`` (:issue:`11159`)

.. _whatsnew_0171.bug_fixes:

Expand Down
16 changes: 12 additions & 4 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2501,11 +2501,19 @@ def dropna(self, axis=0, inplace=False, **kwargs):
'argument "{0}"'.format(list(kwargs.keys())[0]))

axis = self._get_axis_number(axis or 0)
result = remove_na(self)
if inplace:
self._update_inplace(result)

if self._can_hold_na:
result = remove_na(self)
if inplace:
self._update_inplace(result)
else:
return result
else:
return result
if inplace:
# do nothing
pass
else:
return self.copy()

valid = lambda self, inplace=False, **kwargs: self.dropna(inplace=inplace,
**kwargs)
Expand Down
13 changes: 12 additions & 1 deletion pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5117,7 +5117,6 @@ def test_dropna_empty(self):
# invalid axis
self.assertRaises(ValueError, s.dropna, axis=1)


def test_datetime64_tz_dropna(self):
# DatetimeBlock
s = Series([Timestamp('2011-01-01 10:00'), pd.NaT,
Expand All @@ -5140,6 +5139,18 @@ def test_datetime64_tz_dropna(self):
self.assertEqual(result.dtype, 'datetime64[ns, Asia/Tokyo]')
self.assert_series_equal(result, expected)

def test_dropna_no_nan(self):
for s in [Series([1, 2, 3], name='x'),
Series([False, True, False], name='x')]:

result = s.dropna()
self.assert_series_equal(result, s)
self.assertFalse(result is s)

s2 = s.copy()
s2.dropna(inplace=True)
self.assert_series_equal(s2, s)

def test_axis_alias(self):
s = Series([1, 2, np.nan])
assert_series_equal(s.dropna(axis='rows'), s.dropna(axis='index'))
Expand Down