Skip to content

Commit 5f79123

Browse files
JQGohjreback
authored andcommitted
BUG: DatetimeIndex with tz and single column name, return empty df (#19157) (#19330)
1 parent 860c99c commit 5f79123

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ Timezones
431431
- :func:`Timestamp.replace` will now handle Daylight Savings transitions gracefully (:issue:`18319`)
432432
- Bug in tz-aware :class:`DatetimeIndex` where addition/subtraction with a :class:`TimedeltaIndex` or array with ``dtype='timedelta64[ns]'`` was incorrect (:issue:`17558`)
433433
- Bug in :func:`DatetimeIndex.insert` where inserting ``NaT`` into a timezone-aware index incorrectly raised (:issue:`16357`)
434+
- Bug in the :class:`DataFrame` constructor, where tz-aware Datetimeindex and a given column name will result in an empty ``DataFrame`` (:issue:`19157`)
434435

435436
Offsets
436437
^^^^^^^

pandas/core/frame.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,11 @@ def _get_axes(N, K, index=index, columns=columns):
512512
return _arrays_to_mgr([values], columns, index, columns,
513513
dtype=dtype)
514514
elif is_datetimetz(values):
515-
return self._init_dict({0: values}, index, columns, dtype=dtype)
515+
# GH19157
516+
if columns is None:
517+
columns = [0]
518+
return _arrays_to_mgr([values], columns, index, columns,
519+
dtype=dtype)
516520

517521
# by definition an array here
518522
# the dtypes will be coerced to a single dtype

pandas/tests/frame/test_constructors.py

+11
Original file line numberDiff line numberDiff line change
@@ -2092,3 +2092,14 @@ def test_frame_timeseries_to_records(self):
20922092
result['index'].dtype == 'M8[ns]'
20932093

20942094
result = df.to_records(index=False)
2095+
2096+
def test_frame_timeseries_column(self):
2097+
# GH19157
2098+
dr = date_range(start='20130101T10:00:00', periods=3, freq='T',
2099+
tz='US/Eastern')
2100+
result = DataFrame(dr, columns=['timestamps'])
2101+
expected = DataFrame({'timestamps': [
2102+
Timestamp('20130101T10:00:00', tz='US/Eastern'),
2103+
Timestamp('20130101T10:01:00', tz='US/Eastern'),
2104+
Timestamp('20130101T10:02:00', tz='US/Eastern')]})
2105+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)