Skip to content

Commit f7d9cdb

Browse files
committed
BUG: Fixed DataFrame.values for 1 column DataFrame
1 parent b485e5a commit f7d9cdb

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

doc/source/whatsnew/v0.24.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,7 @@ Datetimelike
13141314
- Bug in :class:`DataFrame` with ``datetime64[ns]`` dtype subtracting ``np.datetime64`` object with non-nanosecond unit failing to convert to nanoseconds (:issue:`18874`, :issue:`22163`)
13151315
- Bug in :class:`DataFrame` comparisons against ``Timestamp``-like objects failing to raise ``TypeError`` for inequality checks with mismatched types (:issue:`8932`, :issue:`22163`)
13161316
- Bug in :class:`DataFrame` with mixed dtypes including ``datetime64[ns]`` incorrectly raising ``TypeError`` on equality comparisons (:issue:`13128`, :issue:`22163`)
1317+
- Bug in :attr:`DataFrame.values` returning a :class:`DatetimeIndex` for a single-column ``DataFrame`` with was tz-aware datetime values. Now a 2-D :class:`numpy.ndarray` of :class:`Timestamp` objects is returned (:issue:`24024`)
13171318
- Bug in :meth:`DataFrame.eq` comparison against ``NaT`` incorrectly returning ``True`` or ``NaN`` (:issue:`15697`, :issue:`22163`)
13181319
- Bug in :class:`DatetimeIndex` subtraction that incorrectly failed to raise ``OverflowError`` (:issue:`22492`, :issue:`22508`)
13191320
- Bug in :class:`DatetimeIndex` incorrectly allowing indexing with ``Timedelta`` object (:issue:`20464`)

pandas/core/internals/blocks.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3006,9 +3006,11 @@ def get_values(self, dtype=None):
30063006
# We added an asarray to BlockManager.as_array to work around this.
30073007
values = self.values
30083008
if is_object_dtype(dtype):
3009-
return (values._box_values(values._data)
3010-
.reshape(self.values.shape))
3011-
return self.values
3009+
values = values._box_values(values._data)
3010+
3011+
if self.ndim == 2:
3012+
# Ensure that our shape is correct for DataFrame.
3013+
return values.reshape(1, -1)
30123014

30133015
def _to_json_values(self):
30143016
from pandas import DatetimeIndex

pandas/tests/frame/test_timezones.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,48 @@
99
import numpy as np
1010

1111
import pandas.util.testing as tm
12+
import pandas as pd
1213
from pandas.compat import lrange
1314
from pandas.core.indexes.datetimes import date_range
1415
from pandas.core.dtypes.dtypes import DatetimeTZDtype
1516
from pandas import Series, DataFrame
1617

1718

1819
class TestDataFrameTimezones(object):
20+
21+
def test_frame_values_with_tz(self):
22+
tz = "US/Central"
23+
df = DataFrame({"A": date_range('2000', periods=4, tz=tz)})
24+
result = df.values
25+
expected = np.array([
26+
[pd.Timestamp('2000-01-01', tz=tz)],
27+
[pd.Timestamp('2000-01-02', tz=tz)],
28+
[pd.Timestamp('2000-01-03', tz=tz)],
29+
[pd.Timestamp('2000-01-04', tz=tz)],
30+
])
31+
tm.assert_numpy_array_equal(result, expected)
32+
33+
# two columns, homogenous
34+
35+
df = df.assign(B=df.A)
36+
result = df.values
37+
expected = np.concatenate([expected, expected], axis=1)
38+
tm.assert_numpy_array_equal(result, expected)
39+
40+
# three columns, heterogenous
41+
est = "US/Eastern"
42+
df = df.assign(C=df.A.dt.tz_convert(est))
43+
44+
new = np.array([
45+
[pd.Timestamp('2000-01-01T01:00:00', tz=est)],
46+
[pd.Timestamp('2000-01-02T01:00:00', tz=est)],
47+
[pd.Timestamp('2000-01-03T01:00:00', tz=est)],
48+
[pd.Timestamp('2000-01-04T01:00:00', tz=est)],
49+
])
50+
expected = np.concatenate([expected, new], axis=1)
51+
result = df.values
52+
tm.assert_numpy_array_equal(result, expected)
53+
1954
def test_frame_from_records_utc(self):
2055
rec = {'datum': 1.5,
2156
'begin_time': datetime(2006, 4, 27, tzinfo=pytz.utc)}

pandas/tests/io/json/test_pandas.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,6 @@ def test_tz_is_utc(self):
10131013
dt = ts.to_pydatetime()
10141014
assert dumps(dt, iso_dates=True) == exp
10151015

1016-
@pytest.mark.xfail(reason="TODO-json")
10171016
def test_tz_range_is_utc(self):
10181017
from pandas.io.json import dumps
10191018

0 commit comments

Comments
 (0)