Kozea / Kozea/pygal

DateTimeLine does not accept pandas timestamps

Open
#348 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
2.8k
Forks
419
PR merge metrics
No merged PRs in 30d

Description

I have charts that worked well with pygal v 2.1.1, but fail after an upgrade to 2.3.0.
I pass pandas Timestamps (UTC localized) as x, when I pass datetime objects everything is fine.

The exception:

```
chart = datetimeline.render(is_unicode=True)

File "/usr/local/lib/python2.7/dist-packages/pygal/graph/public.py", line 52, in render

self.setup(**kwargs)

File "/usr/local/lib/python2.7/dist-packages/pygal/graph/base.py", line 189, in setup

[rs for rs in self.raw_series if not rs[1].get('secondary')]) or []

File "/usr/local/lib/python2.7/dist-packages/pygal/graph/base.py", line 160, in prepare_values

self._x_adapt(value[0]),

File "/usr/local/lib/python2.7/dist-packages/pygal/util.py", line 311, in

fun = lambda *args, **kwargs: f(g(*args, **kwargs))

File "/usr/local/lib/python2.7/dist-packages/pygal/graph/time.py", line 35, in datetime_to_timestamp

return timestamp(x)

File "/usr/local/lib/python2.7/dist-packages/pygal/_compat.py", line 97, in timestamp

return (x - datetime(1970, 1, 1, tzinfo=utc)).total_seconds()

File "pandas/tslib.pyx", line 1071, in pandas.tslib._Timestamp.__sub__ (pandas/tslib.c:21031)

TypeError: Timestamp subtraction must have the same timezones or no timezones

```

I propose something like:

```
if isinstance(x, datetime.Datetime):
return (x - datetime(1970, 1, 1, tzinfo=utc)).total_seconds()
else:
return x.value/1e9 #conversion from pandas.Timestamp to EPOCH in seconds
```

This does not lead do an extra dependency for pandas and should solve my issue.

Contributor guide

Open the contributing guide

Research direction

Start in pygal/graph/time.py and pygal/_compat.py, following the datetime_to_timestamp call shown in the traceback. Reproduce the failure with a UTC-localized pandas Timestamp and compare it with a regular datetime, then inspect the existing compatibility tests. Done means DateTimeLine renders both supported timestamp forms without the timezone subtraction error.

Written by the indexing model from the issue text.

Assessment

Tech stack
pandas, python
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.