Skip to content

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

Merged
merged 4 commits into from
Jun 27, 2019
Merged

BUG: Restrict DTA to 1D #27027

merged 4 commits into from
Jun 27, 2019

Conversation

jbrockmendel
Copy link
Member

  • closes #xxxx
  • tests added / passed
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry

Bug identified in #27015. Much less-kludgy patch for NDFrame.rank.

@codecov
Copy link

codecov bot commented Jun 25, 2019

Codecov Report

Merging #27027 into master will decrease coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #27027      +/-   ##
==========================================
- Coverage   91.99%   91.99%   -0.01%     
==========================================
  Files         180      180              
  Lines       50774    50782       +8     
==========================================
+ Hits        46712    46716       +4     
- Misses       4062     4066       +4
Flag Coverage Δ
#multiple 90.63% <100%> (ø) ⬆️
#single 41.85% <37.5%> (-0.06%) ⬇️
Impacted Files Coverage Δ
pandas/core/arrays/datetimes.py 97.8% <100%> (ø) ⬆️
pandas/core/algorithms.py 94.76% <100%> (+0.03%) ⬆️
pandas/io/formats/format.py 97.91% <100%> (ø) ⬆️
pandas/io/gbq.py 88.88% <0%> (-11.12%) ⬇️
pandas/core/frame.py 96.89% <0%> (-0.12%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 83fe8d7...73411ec. Read the comment docs.

@codecov
Copy link

codecov bot commented Jun 25, 2019

Codecov Report

Merging #27027 into master will increase coverage by 0.04%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #27027      +/-   ##
==========================================
+ Coverage   91.99%   92.04%   +0.04%     
==========================================
  Files         180      180              
  Lines       50774    50723      -51     
==========================================
- Hits        46712    46688      -24     
+ Misses       4062     4035      -27
Flag Coverage Δ
#multiple 90.68% <100%> (+0.05%) ⬆️
#single 41.87% <44.44%> (-0.04%) ⬇️
Impacted Files Coverage Δ
pandas/core/arrays/datetimes.py 97.8% <100%> (ø) ⬆️
pandas/core/algorithms.py 94.77% <100%> (+0.04%) ⬆️
pandas/io/formats/format.py 97.91% <100%> (ø) ⬆️
pandas/io/gbq.py 88.88% <0%> (-11.12%) ⬇️
pandas/core/indexing.py 93.3% <0%> (-0.19%) ⬇️
pandas/core/frame.py 96.89% <0%> (-0.12%) ⬇️
pandas/core/ops.py 94.66% <0%> (-0.03%) ⬇️
pandas/core/generic.py 94.18% <0%> (-0.02%) ⬇️
pandas/core/indexes/datetimelike.py 98.14% <0%> (-0.01%) ⬇️
pandas/core/sorting.py 98.35% <0%> (ø) ⬆️
... and 11 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 83fe8d7...8f99a00. Read the comment docs.


with pytest.raises(ValueError, match="Only 1-dimensional"):
# 2-dim
DatetimeArray(arr.reshape(2, 2))
Copy link
Member

Choose a reason for hiding this comment

The 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 ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is currently accepted incorrectly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With latest master:

In [15]: pd.__version__
Out[15]: '0.25.0.dev0+791.gf0919f272'

In [16]: arr = np.array([0, 1, 2, 3], dtype='M8[h]').astype('M8[ns]') 

In [17]: pd.arrays.DatetimeArray(arr.reshape(2, 2)) 
...
~/scipy/pandas/pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.convert_to_tsobject()

TypeError: Cannot convert input [['1970-01-01T00:00:00.000000000' '1970-01-01T01:00:00.000000000']] of type <class 'numpy.ndarray'> to Timestamp

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try changing [17] to res = pd.arrays.DatetimeArray(arr.reshape(2, 2)). I'm pretty sure that error is coming from an attempt to call __repr__.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah .. yes :-)

@@ -1273,6 +1273,11 @@ def format_percentiles(percentiles):

def _is_dates_only(values):
# return a boolean if we are only dates (and don't have a timezone)
if isinstance(values, np.ndarray) and values.ndim > 1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In which case do you run into this?
(I was assuming the format_array is working column by column)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per above, this is hit with 2D ndarray inputs, which ATM are incorrectly accepted but will now raise.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but my question is: when is this actually hit with 2D ndarray input?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question as @jorisvandenbossche

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added an assertion for 1D-ness (in master) and the first test that failed is effectively:

pd.DataFrame({"A": pd.date_range('2016-01-01', periods=3)}).to_csv()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I would rather do a ravel in the code calling it:

fmt = _get_format_datetime64_from_values(values, date_format)
result = tslib.format_array_from_datetime(
i8values.ravel(), tz=getattr(self.values, 'tz', None),
format=fmt, na_rep=na_rep).reshape(i8values.shape)

as it is also done for the actual formatting function right below.

In fact, this is also kind of a bug in our formatting. As the formatting should be done column by column (the frequency of one column should not influence the formatting of another column)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I think is_dates_only is only called with non-ravelled data in one place, so I can move the maybe ravel there and put an assertion in is_dates_only. Is there anything else that should go along with that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you update this?

@@ -104,6 +104,12 @@ def _ensure_data(values, dtype=None):
dtype = values.dtype
else:
# Datetime
if values.ndim > 1:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what exactly hits this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DataFrame.rank with all-datetime64 columns. #27015 has a terrible terrible hack instead of this 5-line workaround,.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we haven't reviewed #27015 not averse, just want to avoid hacks on hacks here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yah, this is distinctly the non-hack solution.

@@ -1273,6 +1273,11 @@ def format_percentiles(percentiles):

def _is_dates_only(values):
# return a boolean if we are only dates (and don't have a timezone)
if isinstance(values, np.ndarray) and values.ndim > 1:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question as @jorisvandenbossche

@gfyoung gfyoung added Bug Datetime Datetime data dtype labels Jun 26, 2019
@@ -104,6 +104,12 @@ def _ensure_data(values, dtype=None):
dtype = values.dtype
else:
# Datetime
if values.ndim > 1:
# Avoid calling the DatetimeIndex constructor as it is 1D only
asi8 = values.view('i8')
Copy link
Contributor

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)

Copy link
Member Author

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.

Copy link
Member

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.

@jreback jreback added this to the 0.25.0 milestone Jun 27, 2019
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would even move it down one step further to to_native_types (there you don't need the if check, other part of the code there is already doing ravel() as well), but no strong feelings if you want to keep it here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from the two minor comments, looks good

@jbrockmendel
Copy link
Member Author

comments addressed and green

@jreback jreback merged commit 8b48f5c into pandas-dev:master Jun 27, 2019
@jbrockmendel jbrockmendel deleted the dtarr2d branch June 27, 2019 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants