Skip to content

Commit 4fee776

Browse files
committed
BUG: dropna() on single column timezone-aware values (#13407)
1 parent 0699659 commit 4fee776

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

912913
Offsets
913914
^^^^^^^

pandas/core/frame.py

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

65846586
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)