Skip to content

Hack fix for BUG - GH24784 #28003

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pandas/plotting/_matplotlib/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,13 @@ def _get_index_freq(data):
weekdays = np.unique(data.index.dayofweek)
if (5 in weekdays) or (6 in weekdays):
freq = None
# This is a hack introduced to avoid tick resolution adjustment issue -
Copy link
Member

Choose a reason for hiding this comment

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

This fix feels too specific. Guessing the fix might be in _use_dynamic_x where the locators are calculated

Copy link
Author

Choose a reason for hiding this comment

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

yes, the _use_dynamic_x is causing the locators to be off by one day if consecutive dates are provided.

# Locators off by one day #24784
elif freq == "D":
freq = None
elif freq.name == "D":
freq = None
# hack logic end - Locators off by one day #24784
return freq


Expand Down
29 changes: 29 additions & 0 deletions pandas/tests/plotting/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,35 @@ def _check_plot_works(f, filterwarnings="always", **kwargs):

return ret

def _check_plot_works_with_continuous_dates(f, filterwarnings="always", **kwargs):
''' first version of the test for - BUG 24784'''
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

ret = None
with warnings.catch_warnings():
warnings.simplefilter(filterwarnings)
try:
try:
fig = kwargs["figure"]
except KeyError:
fig = plt.gcf()

plt.clf()

ax = kwargs.get("ax", fig.add_subplot(211)) # noqa
ret = f(**kwargs)
ret.xaxis.set_major_locator(mdates.DayLocator())
ret.xaxis.set_major_formatter(mdates.DateFormatter("\n%b%d"))

assert_is_valid_plot_return_object(ret)

with ensure_clean(return_filelike=True) as path:
plt.savefig(path)
finally:
tm.close(fig)

return ret

def curpath():
pth, _ = os.path.split(os.path.abspath(__file__))
Expand Down
24 changes: 23 additions & 1 deletion pandas/tests/plotting/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import pandas as pd
from pandas import DataFrame, Series, date_range
from pandas.tests.plotting.common import TestPlotBase, _check_plot_works
from pandas.tests.plotting.common import TestPlotBase, _check_plot_works, _check_plot_works_with_continuous_dates
import pandas.util.testing as tm

import pandas.plotting as plotting
Expand Down Expand Up @@ -731,6 +731,28 @@ def test_dup_datetime_index_plot(self):
s = Series(values, index=index)
_check_plot_works(s.plot)

@pytest.mark.slow
def test_continuous_dates(self):
s = Series(np.arange(10), name="x")
s_err = np.random.randn(10)

ix = date_range("1/1/2018", "1/10/2018", freq="D")
ts = Series(np.arange(10), index=ix, name="x")
ts_err = Series(np.random.randn(10), index=ix)
td_err = DataFrame(randn(10, 2), index=ix, columns=["x", "y"])
ax = _check_plot_works_with_continuous_dates(ts.plot, yerr=ts_err)

self._check_text_labels(ax.get_xticklabels(), ["\nJan01", "\nJan02", "\nJan03", "\nJan04", "\nJan05",
"\nJan06", "\nJan07", "\nJan08", "\nJan09", "\nJan10"])

# check incorrect lengths and types
with pytest.raises(ValueError):
s.plot(yerr=np.arange(11))

s_err = ["zzz"] * 10
with pytest.raises(TypeError):
s.plot(yerr=s_err)

@pytest.mark.slow
def test_errorbar_plot(self):

Expand Down