Skip to content

Commit add37ac

Browse files
JQGohjreback
authored andcommitted
BUG: dropna() on single column timezone-aware values (#13407) (#20422)
1 parent 0fb2eaa commit add37ac

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

doc/source/whatsnew/v0.23.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -904,11 +904,12 @@ Timezones
904904
- :func:`Timestamp.replace` will now handle Daylight Savings transitions gracefully (:issue:`18319`)
905905
- Bug in tz-aware :class:`DatetimeIndex` where addition/subtraction with a :class:`TimedeltaIndex` or array with ``dtype='timedelta64[ns]'`` was incorrect (:issue:`17558`)
906906
- Bug in :func:`DatetimeIndex.insert` where inserting ``NaT`` into a timezone-aware index incorrectly raised (:issue:`16357`)
907-
- Bug in the :class:`DataFrame` constructor, where tz-aware Datetimeindex and a given column name will result in an empty ``DataFrame`` (:issue:`19157`)
907+
- Bug in :class:`DataFrame` constructor, where tz-aware Datetimeindex and a given column name will result in an empty ``DataFrame`` (:issue:`19157`)
908908
- Bug in :func:`Timestamp.tz_localize` where localizing a timestamp near the minimum or maximum valid values could overflow and return a timestamp with an incorrect nanosecond value (:issue:`12677`)
909909
- Bug when iterating over :class:`DatetimeIndex` that was localized with fixed timezone offset that rounded nanosecond precision to microseconds (:issue:`19603`)
910910
- Bug in :func:`DataFrame.diff` that raised an ``IndexError`` with tz-aware values (:issue:`18578`)
911911
- Bug in :func:`melt` that converted tz-aware dtypes to tz-naive (:issue:`15785`)
912+
- Bug in :func:`Dataframe.count` that raised an ``ValueError`` if .dropna() method is invoked for single column timezone-aware values. (:issue:`13407`)
912913

913914
Offsets
914915
^^^^^^^

pandas/core/frame.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -6579,7 +6579,9 @@ def count(self, axis=0, level=None, numeric_only=False):
65796579
# column frames with an extension array
65806580
result = notna(frame).sum(axis=axis)
65816581
else:
6582-
counts = notna(frame.values).sum(axis=axis)
6582+
# GH13407
6583+
series_counts = notna(frame).sum(axis=axis)
6584+
counts = series_counts.values
65836585
result = Series(counts, index=frame._get_agg_axis(axis))
65846586

65856587
return result.astype('int64')

pandas/tests/frame/test_missing.py

+23
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
from numpy import nan, random
99
import numpy as np
1010

11+
import datetime
12+
import dateutil
13+
1114
from pandas.compat import lrange
1215
from pandas import (DataFrame, Series, Timestamp,
1316
date_range, Categorical)
@@ -183,6 +186,26 @@ def test_dropna_multiple_axes(self):
183186
inp.dropna(how='all', axis=(0, 1), inplace=True)
184187
assert_frame_equal(inp, expected)
185188

189+
def test_dropna_tz_aware_datetime(self):
190+
# GH13407
191+
df = DataFrame()
192+
dt1 = datetime.datetime(2015, 1, 1,
193+
tzinfo=dateutil.tz.tzutc())
194+
dt2 = datetime.datetime(2015, 2, 2,
195+
tzinfo=dateutil.tz.tzutc())
196+
df['Time'] = [dt1]
197+
result = df.dropna(axis=0)
198+
expected = DataFrame({'Time': [dt1]})
199+
assert_frame_equal(result, expected)
200+
201+
# Ex2
202+
df = DataFrame({'Time': [dt1, None, np.nan, dt2]})
203+
result = df.dropna(axis=0)
204+
expected = DataFrame([dt1, dt2],
205+
columns=['Time'],
206+
index=[0, 3])
207+
assert_frame_equal(result, expected)
208+
186209
def test_fillna(self):
187210
tf = self.tsframe
188211
tf.loc[tf.index[:5], 'A'] = nan

0 commit comments

Comments
 (0)