diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index 9159c03edee2e..58132fd9395c9 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -903,11 +903,12 @@ Timezones - :func:`Timestamp.replace` will now handle Daylight Savings transitions gracefully (:issue:`18319`) - Bug in tz-aware :class:`DatetimeIndex` where addition/subtraction with a :class:`TimedeltaIndex` or array with ``dtype='timedelta64[ns]'`` was incorrect (:issue:`17558`) - Bug in :func:`DatetimeIndex.insert` where inserting ``NaT`` into a timezone-aware index incorrectly raised (:issue:`16357`) -- Bug in the :class:`DataFrame` constructor, where tz-aware Datetimeindex and a given column name will result in an empty ``DataFrame`` (:issue:`19157`) +- Bug in :class:`DataFrame` constructor, where tz-aware Datetimeindex and a given column name will result in an empty ``DataFrame`` (:issue:`19157`) - 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`) - Bug when iterating over :class:`DatetimeIndex` that was localized with fixed timezone offset that rounded nanosecond precision to microseconds (:issue:`19603`) - Bug in :func:`DataFrame.diff` that raised an ``IndexError`` with tz-aware values (:issue:`18578`) - Bug in :func:`melt` that converted tz-aware dtypes to tz-naive (:issue:`15785`) +- Bug in :func:`Dataframe.count` that raised an ``ValueError`` if .dropna() method is invoked for single column timezone-aware values. (:issue:`13407`) Offsets ^^^^^^^ diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 9b09c87689762..b1217bec69655 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -6578,7 +6578,9 @@ def count(self, axis=0, level=None, numeric_only=False): # column frames with an extension array result = notna(frame).sum(axis=axis) else: - counts = notna(frame.values).sum(axis=axis) + # GH13407 + series_counts = notna(frame).sum(axis=axis) + counts = series_counts.values result = Series(counts, index=frame._get_agg_axis(axis)) return result.astype('int64') diff --git a/pandas/tests/frame/test_missing.py b/pandas/tests/frame/test_missing.py index 2e4e8b9582cf6..668eae21c664f 100644 --- a/pandas/tests/frame/test_missing.py +++ b/pandas/tests/frame/test_missing.py @@ -8,6 +8,9 @@ from numpy import nan, random import numpy as np +import datetime +import dateutil + from pandas.compat import lrange from pandas import (DataFrame, Series, Timestamp, date_range, Categorical) @@ -183,6 +186,26 @@ def test_dropna_multiple_axes(self): inp.dropna(how='all', axis=(0, 1), inplace=True) assert_frame_equal(inp, expected) + def test_dropna_tz_aware_datetime(self): + # GH13407 + df = DataFrame() + dt1 = datetime.datetime(2015, 1, 1, + tzinfo=dateutil.tz.tzutc()) + dt2 = datetime.datetime(2015, 2, 2, + tzinfo=dateutil.tz.tzutc()) + df['Time'] = [dt1] + result = df.dropna(axis=0) + expected = DataFrame({'Time': [dt1]}) + assert_frame_equal(result, expected) + + # Ex2 + df = DataFrame({'Time': [dt1, None, np.nan, dt2]}) + result = df.dropna(axis=0) + expected = DataFrame([dt1, dt2], + columns=['Time'], + index=[0, 3]) + assert_frame_equal(result, expected) + def test_fillna(self): tf = self.tsframe tf.loc[tf.index[:5], 'A'] = nan