Skip to content

BUG: record warnings related to DatetimeIndex #37193

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 2 commits into from
Oct 23, 2020

Conversation

ivanovmg
Copy link
Member

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

This PR handles warnings emitted when running tests in pandas/tests/plotting/test_datetimelike.py.
Warnings looked like this:

pandas/tests/plotting/test_datetimelike.py::TestTSPlot::test_irreg_dtypes
  /workspaces/pandas/pandas/core/frame.py:3216: FutureWarning: Automatically casting object-dtype Index of datetimes to DatetimeIndex is deprecated and will be removed in a future version.  Explicitly cast to DatetimeIndex instead.
    return klass(values, index=self.index, name=name, fastpath=True)

Presumably it started after implementing this: #36697

The reason for not using tm.assert_produces_warning is that when I use it,
then I get an error that the warning is raised with the incorrect stacklevel.

@ivanovmg ivanovmg requested a review from jbrockmendel October 17, 2020 15:39
df2.plot(ax=ax)
diffs = Series(ax.get_lines()[0].get_xydata()[:, 0]).diff()
assert (np.fabs(diffs[1:] - sec) < 1e-8).all()
with warnings.catch_warnings(record=True) as w:
Copy link
Contributor

Choose a reason for hiding this comment

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

I would rather use assert_produces_warning than custom code (and just disable the stack level check). pls also coment that the explict cast to object is causing.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

df = DataFrame(np.random.randn(len(idx), 3), idx)
_, ax = self.plt.subplots()
_check_plot_works(df.plot, ax=ax)
with warnings.catch_warnings(record=True) as w:
Copy link
Contributor

Choose a reason for hiding this comment

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

same comment as above

@jreback jreback added the Datetime Datetime data dtype label Oct 17, 2020
@ivanovmg ivanovmg requested a review from jreback October 18, 2020 14:05
@jreback jreback added the Visualization plotting label Oct 23, 2020
@jreback jreback added this to the 1.2 milestone Oct 23, 2020
@jreback jreback merged commit 23c41bd into pandas-dev:master Oct 23, 2020
@jreback
Copy link
Contributor

jreback commented Oct 23, 2020

thanks @ivanovmg

@jorisvandenbossche
Copy link
Member

I am not fully sure this is the correct way to deal with those warnings.

So the warnings can be reproduced with this snippet:

idx = pd.date_range("2012-6-22 21:59:51", freq="S", periods=100)
df = pd.DataFrame(np.random.randn(len(idx), 2), index=idx)
df.index = df.index.astype(object)

df.plot(ax=ax)

In this case, the user is not doing any conversion of the index themselves, so at best the warning is quite confusing.

Further, the question is if the behaviour will actually change once we enforce the deprecation. Because if not, then the warning is also useless (it's not warning about something that will change).
Based on a quick check (running the snippet above with master vs with the the code in _set_axis related to the warning commented out), I don't think there is actually any change in behaviour.

@jreback
Copy link
Contributor

jreback commented Oct 23, 2020

I am not fully sure this is the correct way to deal with those warnings.

So the warnings can be reproduced with this snippet:


idx = pd.date_range("2012-6-22 21:59:51", freq="S", periods=100)

df = pd.DataFrame(np.random.randn(len(idx), 2), index=idx)

df.index = df.index.astype(object)



df.plot(ax=ax)

In this case, the user is not doing any conversion of the index themselves, so at best the warning is quite confusing.

Further, the question is if the behaviour will actually change once we enforce the deprecation. Because if not, then the warning is also useless (it's not warning about something that will change).

Based on a quick check (running the snippet above with master vs with the the code in _set_axis related to the warning commented out), I don't think there is actually any change in behaviour.

there is an explicit astype here
so this is just testing that the user is getting the warning which AFACTA is correct

@jorisvandenbossche
Copy link
Member

there is an explicit astype here

The warning is not coming from there, but from the plot() call.

So yes it is plotting a dataframe with an object-dtype index. But 1) the warning is not clear about that and 2) we actually support plotting object dtype values, so there shouldn't necessarily be a warning at all.

@jreback
Copy link
Contributor

jreback commented Oct 23, 2020

ok i guess - why don't u open a new issue

this was just testing that we get a warning

JulianWgs pushed a commit to JulianWgs/pandas that referenced this pull request Oct 26, 2020
kesmit13 pushed a commit to kesmit13/pandas that referenced this pull request Nov 2, 2020
@ivanovmg ivanovmg deleted the warnings/plotting-datetime branch November 6, 2020 15:32
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this pull request Dec 24, 2020
jreback pushed a commit that referenced this pull request Dec 25, 2020
…to DatetimeIndex in the Series constructor (23598)" (#38679)

* Revert "DEPR: is_all_dates (#36697)"

This reverts commit 90a6135.

* revert Deprecated casting an object-dtype index of ``datetime`` objects to :class:`.DatetimeIndex` in the :class:`Series` constructor (:issue:`23598`) only

* Revert "BUG: record warnings related to DatetimeIndex (#37193)"

This reverts commit 23c41bd.

* remove assert_produces_warning added in TST/REF: collect tests by method, some misplaced #37354
luckyvs1 pushed a commit to luckyvs1/pandas that referenced this pull request Jan 20, 2021
…to DatetimeIndex in the Series constructor (23598)" (pandas-dev#38679)

* Revert "DEPR: is_all_dates (pandas-dev#36697)"

This reverts commit 90a6135.

* revert Deprecated casting an object-dtype index of ``datetime`` objects to :class:`.DatetimeIndex` in the :class:`Series` constructor (:issue:`23598`) only

* Revert "BUG: record warnings related to DatetimeIndex (pandas-dev#37193)"

This reverts commit 23c41bd.

* remove assert_produces_warning added in TST/REF: collect tests by method, some misplaced pandas-dev#37354
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Visualization plotting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants