Skip to content
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: df.plot() with FixedOffset timezones #17173

Closed
deadlybulb opened this issue Aug 4, 2017 · 6 comments

Comments

@deadlybulb
Copy link

commented Aug 4, 2017

Code Sample, a copy-pastable example if possible

The following example will trigger an exception:

import pandas as pd
import datetime

data = []
now = datetime.datetime.now(tz=datetime.timezone.utc)
for i in range(10):
    data.append(dict(date=now + datetime.timedelta(days=i), data=i))

df = pd.DataFrame(data, index=[x['date'] for x in data])

df.plot()

This should fail with the error:

pandas/_libs/tslib.pyx in pandas._libs.tslib.dates_normalized (pandas/_libs/tslib.c:88136)()

AttributeError: 'datetime.timezone' object has no attribute '_transition_info'

Problem description

The "else" branch in this code will fail on instances of datetime.timezone which don't have a _transition_info internal field, which is true with objects like datetime.timezone.utc.

The code on master looks like this:

    else:
        trans, deltas, typ = _get_dst_info(tz)

        for i in range(n):
            # Adjust datetime64 timestamp, recompute datetimestruct
            pos = trans.searchsorted(stamps[i]) - 1
            inf = tz._transition_info[pos]

            pandas_datetime_to_datetimestruct(stamps[i] + deltas[pos],
                                              PANDAS_FR_ns, &dts)
            if (dts.hour + dts.min + dts.sec + dts.us) > 0:
                return False

The problem is the setting of inf and its reference to _transition_info, but if I'm reading this code correctly, this is dead code anyway. With that line removed, this code should work with things like datetime.timezone.utc again.

Expected Output

I'd expect to be able to plot off timestamps which use datetime.timezone.utc as their timezone.

Output of pd.show_versions()

>>> pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.1.final.0
python-bits: 64
OS: Linux
OS-release: 4.10.0-27-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.20.2
pytest: None
pip: 9.0.1
setuptools: 27.2.0
Cython: None
numpy: 1.13.0
scipy: 0.19.1
xarray: None
IPython: 6.1.0
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None

@jreback

This comment has been minimized.

Copy link
Contributor

commented Aug 4, 2017

can u show an example where this actually fails

@deadlybulb

This comment has been minimized.

Copy link
Author

commented Aug 4, 2017

Example added above

@jreback

This comment has been minimized.

Copy link
Contributor

commented Aug 6, 2017

this is a pretty new API IIRC. We don't support this, only pytz and dateutil for timezones. xref to #15986.

If someone wants to add support (and to be honest, its mostly testing), go for it.

@jreback jreback added this to the Next Major Release milestone Aug 6, 2017

@mroeschke mroeschke changed the title tslib.dates_normalized() depends on internal pytz API tslib.dates_normalized() depends on internal pytz API (support datetime.timezone objects) Jul 26, 2018

@mroeschke

This comment has been minimized.

Copy link
Member

commented Nov 23, 2018

This now raises:

ValueError: view limit minimum -36851.5436251915 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units
@mroeschke

This comment has been minimized.

Copy link
Member

commented Feb 3, 2019

Actually this is more of an issue with FixedOffset timezones now.

In [10]: t = pytz.FixedOffset(30)

In [11]: t
Out[11]: pytz.FixedOffset(30)

In [12]: df.index = df.index.tz_convert(t)

In [13]: df['date'] = df['date'].dt.tz_convert(t)

In [14]: df
Out[14]:
                                  data                             date
2019-02-03 18:21:03.191561+00:30     0 2019-02-03 18:21:03.191561+00:30
2019-02-04 18:21:03.191561+00:30     1 2019-02-04 18:21:03.191561+00:30
2019-02-05 18:21:03.191561+00:30     2 2019-02-05 18:21:03.191561+00:30
2019-02-06 18:21:03.191561+00:30     3 2019-02-06 18:21:03.191561+00:30
2019-02-07 18:21:03.191561+00:30     4 2019-02-07 18:21:03.191561+00:30
2019-02-08 18:21:03.191561+00:30     5 2019-02-08 18:21:03.191561+00:30
2019-02-09 18:21:03.191561+00:30     6 2019-02-09 18:21:03.191561+00:30
2019-02-10 18:21:03.191561+00:30     7 2019-02-10 18:21:03.191561+00:30
2019-02-11 18:21:03.191561+00:30     8 2019-02-11 18:21:03.191561+00:30
2019-02-12 18:21:03.191561+00:30     9 2019-02-12 18:21:03.191561+00:30

In [17]: df.plot()
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6

@mroeschke mroeschke changed the title tslib.dates_normalized() depends on internal pytz API (support datetime.timezone objects) BUG: df.plot() with FixedOffset timezones Feb 3, 2019

@mroeschke

This comment has been minimized.

Copy link
Member

commented Jul 12, 2019

I don't think the following lines are needed since we handle datetime.timezone instances now.

diff --git a/pandas/plotting/_matplotlib/converter.py b/pandas/plotting/_matplotlib/converter.py
index b20dd3212..15648d59c 100644
--- a/pandas/plotting/_matplotlib/converter.py
+++ b/pandas/plotting/_matplotlib/converter.py
@@ -324,9 +324,6 @@ class DatetimeConverter(dates.DateConverter):
 class PandasAutoDateFormatter(dates.AutoDateFormatter):
     def __init__(self, locator, tz=None, defaultfmt="%Y-%m-%d"):
         dates.AutoDateFormatter.__init__(self, locator, tz, defaultfmt)
-        # matplotlib.dates._UTC has no _utcoffset called by pandas
-        if self._tz is dates.UTC:
-            self._tz._utcoffset = self._tz.utcoffset(None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.