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

num2date ignoring tz in v1.5.0 #5543

Closed
brendene opened this issue Nov 22, 2015 · 4 comments
Closed

num2date ignoring tz in v1.5.0 #5543

brendene opened this issue Nov 22, 2015 · 4 comments
Milestone

Comments

@brendene
Copy link

The behavior of matplotlib.dates.num2date changed between stable versions 1.4.3 and 1.5.

In version 1.5 the implementation of helper function, _from_ordinalf ignores the tz argument and always returns datetimes in UTC.

def _from_ordinalf(x, tz=None):
    """                                                                                                                                                         
    Convert Gregorian float of the date, preserving hours, minutes,                                                                                             
    seconds and microseconds.  Return value is a :class:`datetime`.                                                                                             

    The input date `x` is a float in ordinal days at UTC, and the output will                                                                                   
    be the specified :class:`datetime` object corresponding to that time in                                                                                     
    timezone `tz`, or if `tz` is `None`, in the timezone specified in                                                                                           
    `rcParams['timezone']`.                                                                                                                                     
    """
    if tz is None:
        tz = _get_rc_timezone()

    ix = int(x)
    dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC)

    remainder = float(x) - ix

    # Round down to the nearest microsecond.                                                                                                                    
    dt += datetime.timedelta(microseconds=int(remainder * MUSECONDS_PER_DAY))

    # Compensate for rounding errors                                                                                                                            
    if dt.microsecond < 10:
        dt = dt.replace(microsecond=0)
    elif dt.microsecond > 999990:
        dt += datetime.timedelta(microseconds=1e6 - dt.microsecond)

    return dt

In version 1.4.3 this is handled correctly.

@tacaswell tacaswell added this to the Critical bugfix release (1.5.1) milestone Nov 23, 2015
@tacaswell
Copy link
Member

attn @pganssle

@tacaswell
Copy link
Member

@brendene Can you provide a minimal example of the usage of this?

@brendene
Copy link
Author

I discovered this when using Pandas (0.17.1) and matplotlib (1.5.0) while trying to plot time-series data where the x-axis is timezone aware datetime. No matter what I did I could not change the x-axis to use a timezone other than UTC.

Here is a simple example which I believe causes the plotting issue.

Version 1.5.0

In [1]: import matplotlib.dates, datetime, pytz

In [2]: x = datetime.datetime.utcfromtimestamp(1448154154)

In [3]: y = matplotlib.dates.date2num(x)

In [4]: print matplotlib.dates.num2date(y), matplotlib.dates.num2date(y, tz=pytz.timezone("US/Eastern"))
2015-11-22 01:02:34+00:00 2015-11-22 01:02:34+00:00

Version 1.4.3

In [1]: import matplotlib.dates, datetime, pytz

In [2]: x = datetime.datetime.utcfromtimestamp(1448154154)

In [3]: y = matplotlib.dates.date2num(x)

In [4]: print matplotlib.dates.num2date(y), matplotlib.dates.num2date(y, tz=pytz.timezone("US/Eastern"))
2015-11-22 01:02:34+00:00 2015-11-21 20:02:34-05:00

@mdboom
Copy link
Member

mdboom commented Nov 23, 2015

git bisect points at one of these three commits:

The first bad commit could be any of:
5af753c
ee7931c
4b0d94b

brendene pushed a commit to brendene/matplotlib that referenced this issue Nov 28, 2015
tacaswell added a commit that referenced this issue Dec 14, 2015
Fix #5543: Handle timezone in num2date
@mdboom mdboom closed this as completed in e57053d Dec 14, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants