Skip to content

Commit 1f2d356

Browse files
mroeschkePingviinituutti
authored andcommitted
BUG: Append DataFrame to Series with dateutil timezone (pandas-dev#23685)
1 parent 9911277 commit 1f2d356

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,7 @@ Reshaping
14141414
- Bug in :func:`pandas.concat` when concatenating a multicolumn DataFrame with tz-aware data against a DataFrame with a different number of columns (:issue:`22796`)
14151415
- Bug in :func:`merge_asof` where confusing error message raised when attempting to merge with missing values (:issue:`23189`)
14161416
- Bug in :meth:`DataFrame.nsmallest` and :meth:`DataFrame.nlargest` for dataframes that have a :class:`MultiIndex` for columns (:issue:`23033`).
1417+
- Bug in :meth:`DataFrame.append` with a :class:`Series` with a dateutil timezone would raise a ``TypeError`` (:issue:`23682`)
14171418

14181419
.. _whatsnew_0240.bug_fixes.sparse:
14191420

pandas/_libs/lib.pyx

+12-11
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ cdef extern from "src/parse_helper.h":
4848
int floatify(object, float64_t *result, int *maybe_int) except -1
4949

5050
cimport util
51-
from util cimport (is_nan,
52-
UINT8_MAX, UINT64_MAX, INT64_MAX, INT64_MIN)
51+
from util cimport is_nan, UINT64_MAX, INT64_MAX, INT64_MIN
5352

5453
from tslib import array_to_datetime
5554
from tslibs.nattype cimport NPY_NAT
@@ -1642,20 +1641,22 @@ def is_datetime_with_singletz_array(values: ndarray) -> bool:
16421641

16431642
if n == 0:
16441643
return False
1645-
1644+
# Get a reference timezone to compare with the rest of the tzs in the array
16461645
for i in range(n):
16471646
base_val = values[i]
16481647
if base_val is not NaT:
16491648
base_tz = get_timezone(getattr(base_val, 'tzinfo', None))
1650-
1651-
for j in range(i, n):
1652-
val = values[j]
1653-
if val is not NaT:
1654-
tz = getattr(val, 'tzinfo', None)
1655-
if not tz_compare(base_tz, tz):
1656-
return False
16571649
break
16581650

1651+
for j in range(i, n):
1652+
# Compare val's timezone with the reference timezone
1653+
# NaT can coexist with tz-aware datetimes, so skip if encountered
1654+
val = values[j]
1655+
if val is not NaT:
1656+
tz = getattr(val, 'tzinfo', None)
1657+
if not tz_compare(base_tz, tz):
1658+
return False
1659+
16591660
return True
16601661

16611662

@@ -2045,7 +2046,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=0,
20452046

20462047
# we try to coerce datetime w/tz but must all have the same tz
20472048
if seen.datetimetz_:
2048-
if len({getattr(val, 'tzinfo', None) for val in objects}) == 1:
2049+
if is_datetime_with_singletz_array(objects):
20492050
from pandas import DatetimeIndex
20502051
return DatetimeIndex(objects)
20512052
seen.object_ = 1

pandas/tests/reshape/test_concat.py

+15
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,21 @@ def test_append_missing_column_proper_upcast(self, sort):
10101010
assert appended['A'].dtype == 'f8'
10111011
assert appended['B'].dtype == 'O'
10121012

1013+
def test_append_empty_frame_to_series_with_dateutil_tz(self):
1014+
# GH 23682
1015+
date = Timestamp('2018-10-24 07:30:00', tz=dateutil.tz.tzutc())
1016+
s = Series({'date': date, 'a': 1.0, 'b': 2.0})
1017+
df = DataFrame(columns=['c', 'd'])
1018+
result = df.append(s, ignore_index=True)
1019+
expected = DataFrame([[np.nan, np.nan, 1., 2., date]],
1020+
columns=['c', 'd', 'a', 'b', 'date'])
1021+
# These columns get cast to object after append
1022+
object_cols = ['c', 'd', 'date']
1023+
expected.loc[:, object_cols] = expected.loc[:, object_cols].astype(
1024+
object
1025+
)
1026+
assert_frame_equal(result, expected)
1027+
10131028

10141029
class TestConcatenate(ConcatenateBase):
10151030

0 commit comments

Comments
 (0)