-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Restrict DTA to 1D #27027
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: Restrict DTA to 1D #27027
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1273,6 +1273,8 @@ def format_percentiles(percentiles): | |
|
||
def _is_dates_only(values): | ||
# return a boolean if we are only dates (and don't have a timezone) | ||
assert values.ndim == 1 | ||
|
||
values = DatetimeIndex(values) | ||
if values.tz is not None: | ||
return False | ||
|
@@ -1324,6 +1326,12 @@ def _get_format_datetime64(is_dates_only, nat_rep='NaT', date_format=None): | |
|
||
def _get_format_datetime64_from_values(values, date_format): | ||
""" given values and a date_format, return a string format """ | ||
|
||
if isinstance(values, np.ndarray) and values.ndim > 1: | ||
# We don't actaully care about the order of values, and DatetimeIndex | ||
# only accepts 1D values | ||
values = values.ravel() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would even move it down one step further to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
is_dates_only = _is_dates_only(values) | ||
if is_dates_only: | ||
return date_format or "%Y-%m-%d" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,18 @@ | |
|
||
|
||
class TestDatetimeArrayConstructor: | ||
|
||
def test_only_1dim_accepted(self): | ||
arr = np.array([0, 1, 2, 3], dtype='M8[h]').astype('M8[ns]') | ||
|
||
with pytest.raises(ValueError, match="Only 1-dimensional"): | ||
# 2-dim | ||
DatetimeArray(arr.reshape(2, 2)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be clear: this already fails currently, right? You are mainly catching the error early / providing a better error message ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, this is currently accepted incorrectly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With latest master:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try changing [17] to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah .. yes :-) |
||
|
||
with pytest.raises(ValueError, match="Only 1-dimensional"): | ||
# 0-dim | ||
DatetimeArray(arr[[0]].squeeze()) | ||
|
||
def test_freq_validation(self): | ||
# GH#24623 check that invalid instances cannot be created with the | ||
# public constructor | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that ensure_data should only take 1d input at all times. Is there a case where it does not? (nb we shoul dprob document / type this)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this gets called with 2D values from
DataFrame.rank
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know if it is only rank? Because if so, it might be useful to add that as a comment for somebody later reading the code and wondering the same question where 2D things are passed to this.