Skip to content

Commit 8b48f5c

Browse files
jbrockmendeljreback
authored andcommitted
BUG: Restrict DTA to 1D (#27027)
1 parent f513191 commit 8b48f5c

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

pandas/core/algorithms.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
from pandas.core.dtypes.common import (
1818
ensure_float64, ensure_int64, ensure_object, ensure_platform_int,
1919
ensure_uint64, is_array_like, is_bool_dtype, is_categorical_dtype,
20-
is_complex_dtype, is_datetime64_any_dtype, is_datetime64tz_dtype,
21-
is_datetimelike, is_extension_array_dtype, is_float_dtype, is_integer,
22-
is_integer_dtype, is_interval_dtype, is_list_like, is_numeric_dtype,
23-
is_object_dtype, is_period_dtype, is_scalar, is_signed_integer_dtype,
24-
is_sparse, is_timedelta64_dtype, is_unsigned_integer_dtype,
25-
needs_i8_conversion)
20+
is_complex_dtype, is_datetime64_any_dtype, is_datetime64_ns_dtype,
21+
is_datetime64tz_dtype, is_datetimelike, is_extension_array_dtype,
22+
is_float_dtype, is_integer, is_integer_dtype, is_interval_dtype,
23+
is_list_like, is_numeric_dtype, is_object_dtype, is_period_dtype,
24+
is_scalar, is_signed_integer_dtype, is_sparse, is_timedelta64_dtype,
25+
is_unsigned_integer_dtype, needs_i8_conversion)
2626
from pandas.core.dtypes.generic import ABCIndex, ABCIndexClass, ABCSeries
2727
from pandas.core.dtypes.missing import isna, na_value_for_dtype
2828

@@ -105,6 +105,13 @@ def _ensure_data(values, dtype=None):
105105
dtype = values.dtype
106106
else:
107107
# Datetime
108+
if values.ndim > 1 and is_datetime64_ns_dtype(values):
109+
# Avoid calling the DatetimeIndex constructor as it is 1D only
110+
# Note: this is reached by DataFrame.rank calls GH#27027
111+
asi8 = values.view('i8')
112+
dtype = values.dtype
113+
return asi8, dtype, 'int64'
114+
108115
from pandas import DatetimeIndex
109116
values = DatetimeIndex(values)
110117
dtype = values.dtype

pandas/core/arrays/datetimes.py

+2
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ def __init__(self, values, dtype=_NS_DTYPE, freq=None, copy=False):
309309
"ndarray, or Series or Index containing one of those."
310310
)
311311
raise ValueError(msg.format(type(values).__name__))
312+
if values.ndim != 1:
313+
raise ValueError("Only 1-dimensional input arrays are supported.")
312314

313315
if values.dtype == 'i8':
314316
# for compat with datetime/timedelta/period shared methods,

pandas/io/formats/format.py

+8
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,8 @@ def format_percentiles(percentiles):
12741274

12751275
def _is_dates_only(values):
12761276
# return a boolean if we are only dates (and don't have a timezone)
1277+
assert values.ndim == 1
1278+
12771279
values = DatetimeIndex(values)
12781280
if values.tz is not None:
12791281
return False
@@ -1325,6 +1327,12 @@ def _get_format_datetime64(is_dates_only, nat_rep='NaT', date_format=None):
13251327

13261328
def _get_format_datetime64_from_values(values, date_format):
13271329
""" given values and a date_format, return a string format """
1330+
1331+
if isinstance(values, np.ndarray) and values.ndim > 1:
1332+
# We don't actaully care about the order of values, and DatetimeIndex
1333+
# only accepts 1D values
1334+
values = values.ravel()
1335+
13281336
is_dates_only = _is_dates_only(values)
13291337
if is_dates_only:
13301338
return date_format or "%Y-%m-%d"

pandas/tests/arrays/test_datetimes.py

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@
1515

1616

1717
class TestDatetimeArrayConstructor:
18+
19+
def test_only_1dim_accepted(self):
20+
arr = np.array([0, 1, 2, 3], dtype='M8[h]').astype('M8[ns]')
21+
22+
with pytest.raises(ValueError, match="Only 1-dimensional"):
23+
# 2-dim
24+
DatetimeArray(arr.reshape(2, 2))
25+
26+
with pytest.raises(ValueError, match="Only 1-dimensional"):
27+
# 0-dim
28+
DatetimeArray(arr[[0]].squeeze())
29+
1830
def test_freq_validation(self):
1931
# GH#24623 check that invalid instances cannot be created with the
2032
# public constructor

0 commit comments

Comments
 (0)