Skip to content

Commit cbe3c81

Browse files
committed
BUG: dropna() on single column timezone-aware values (pandas-dev#13407)
1 parent 01882ba commit cbe3c81

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

doc/source/whatsnew/v0.23.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -902,11 +902,12 @@ Timezones
902902
- :func:`Timestamp.replace` will now handle Daylight Savings transitions gracefully (:issue:`18319`)
903903
- Bug in tz-aware :class:`DatetimeIndex` where addition/subtraction with a :class:`TimedeltaIndex` or array with ``dtype='timedelta64[ns]'`` was incorrect (:issue:`17558`)
904904
- Bug in :func:`DatetimeIndex.insert` where inserting ``NaT`` into a timezone-aware index incorrectly raised (:issue:`16357`)
905-
- Bug in the :class:`DataFrame` constructor, where tz-aware Datetimeindex and a given column name will result in an empty ``DataFrame`` (:issue:`19157`)
905+
- Bug in :class:`DataFrame` constructor, where tz-aware Datetimeindex and a given column name will result in an empty ``DataFrame`` (:issue:`19157`)
906906
- 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`)
907907
- Bug when iterating over :class:`DatetimeIndex` that was localized with fixed timezone offset that rounded nanosecond precision to microseconds (:issue:`19603`)
908908
- Bug in :func:`DataFrame.diff` that raised an ``IndexError`` with tz-aware values (:issue:`18578`)
909909
- Bug in :func:`melt` that converted tz-aware dtypes to tz-naive (:issue:`15785`)
910+
- Bug in :func:`Dataframe.count` that raised an ``ValueError`` if .dropna() method is invoked for single column timezone-aware values. (:issue:`13407`)
910911

911912
Offsets
912913
^^^^^^^

pandas/core/frame.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -6487,7 +6487,9 @@ def count(self, axis=0, level=None, numeric_only=False):
64876487
# column frames with an extension array
64886488
result = notna(frame).sum(axis=axis)
64896489
else:
6490-
counts = notna(frame.values).sum(axis=axis)
6490+
# GH13407
6491+
series_counts = notna(frame).sum(axis=axis)
6492+
counts = series_counts.values
64916493
result = Series(counts, index=frame._get_agg_axis(axis))
64926494

64936495
return result.astype('int64')

pandas/tests/frame/test_missing.py

+30
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,33 @@ 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+
# Example reported by GH13407
192+
df = DataFrame()
193+
df['Time'] = [datetime.datetime(
194+
2015, 1, 1, tzinfo=dateutil.tz.tzutc())]
195+
result = df.dropna(axis=0)
196+
expected = DataFrame({'Time': [datetime.datetime(
197+
2015, 1, 1, tzinfo=dateutil.tz.tzutc())]})
198+
assert_frame_equal(result, expected)
199+
200+
# Ex2
201+
df2 = DataFrame({'Time': [datetime.datetime(
202+
2015, 1, 1, tzinfo=dateutil.tz.tzutc()),
203+
None,
204+
np.nan,
205+
datetime.datetime(2015, 2, 2,
206+
tzinfo=dateutil.tz.tzutc())]})
207+
result2 = df2.dropna(axis=0)
208+
expected2 = DataFrame([datetime.datetime(
209+
2015, 1, 1, tzinfo=dateutil.tz.tzutc()),
210+
datetime.datetime(2015, 2, 2,
211+
tzinfo=dateutil.tz.tzutc())],
212+
columns=['Time'],
213+
index=[0, 3])
214+
assert_frame_equal(result2, expected2)
215+
186216
def test_fillna(self):
187217
tf = self.tsframe
188218
tf.loc[tf.index[:5], 'A'] = nan

0 commit comments

Comments
 (0)