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

Tz-aware datetime.datetime cannot be converted to datetime64 unless utc=True #4250

Closed
3 tasks done
psinger opened this issue Jan 19, 2018 · 10 comments
Closed
3 tasks done
Labels
inactive Inactive for >= 30 days

Comments

@psinger
Copy link

psinger commented Jan 19, 2018

Make sure these boxes are checked before submitting your issue - thank you!

  • I have checked the superset logs for python stacktraces and included it here as text if any
  • I have reproduced the issue with at least the latest released version of superset
  • I have checked the issue tracker for the same issue and I haven't found one similar

Superset version

current master

Expected results

chart

Actual results

error: Tz-aware datetime.datetime cannot be converted to datetime64 unless utc=True

Steps to reproduce

Use date column in postgresql

I am receiving the error Tz-aware datetime.datetime cannot be converted to datetime64 unless utc=True when I try to use a timestamp column that is a date column in postgresql. Timestamp columns work fine. Python/pandas trace:

2018-01-19 16:34:23,678:ERROR:root:Tz-aware datetime.datetime cannot be converted to datetime64 unless utc=True
Traceback (most recent call last):
File "/home/x/anaconda3/lib/python3.6/site-packages/pandas/core/tools/datetimes.py", line 444, in _convert_listlike
values, tz = tslib.datetime_to_datetime64(arg)
File "pandas/_libs/tslib.pyx", line 1796, in pandas._libs.tslib.datetime_to_datetime64 (pandas/_libs/tslib.c:33086)
ValueError: Array must be all same time zone

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/x/tmp/incubator-superset/superset/viz.py", line 273, in get_payload
df = self.get_df(query_obj)
File "/home/x/tmp/incubator-superset/superset/viz.py", line 124, in get_df
df[DTTM_ALIAS], utc=False, format=timestamp_format)
File "/home/x/anaconda3/lib/python3.6/site-packages/pandas/core/tools/datetimes.py", line 509, in to_datetime
values = _convert_listlike(arg._values, False, format)
File "/home/x/anaconda3/lib/python3.6/site-packages/pandas/core/tools/datetimes.py", line 447, in _convert_listlike
raise e
File "/home/x/anaconda3/lib/python3.6/site-packages/pandas/core/tools/datetimes.py", line 435, in _convert_listlike
require_iso8601=require_iso8601
File "pandas/_libs/tslib.pyx", line 2355, in pandas._libs.tslib.array_to_datetime (pandas/_libs/tslib.c:46617)
File "pandas/_libs/tslib.pyx", line 2401, in pandas._libs.tslib.array_to_datetime (pandas/_libs/tslib.c:43251)
ValueError: Tz-aware datetime.datetime cannot be converted to datetime64 unless utc=True

@xrmx
Copy link
Contributor

xrmx commented Jan 19, 2018

which database is this?

@psinger
Copy link
Author

psinger commented Jan 19, 2018

@xrmx Postgresql

@psinger
Copy link
Author

psinger commented Jan 19, 2018

If one changes pd.to_datetime(df[DTTM_ALIAS], utc=False, format=timestamp_format) in viz.py to pd.to_datetime(df[DTTM_ALIAS], utc=True, format=timestamp_format), it works. But that does not seem to be the best solution as datetime might be tz aware.

@habalux
Copy link
Contributor

habalux commented Jan 30, 2018

Hi,

This seems to happen because of postgresql DATE_TRUNC() function which returns a "timestamp with time zone" when given a "date" column and therefore causing the error with pandas. I fixed it by casting the function call back to "timestamp without time zone" in db_engine_specs.py - probably not the best solution either but at least it was enough to circumvent the problem for the time being.

@mistercrunch
Copy link
Member

@habalux mind opening a PR with your change for reference? Others may want to apply the same patch.

@habalux
Copy link
Contributor

habalux commented Jan 31, 2018

@mistercrunch PR submitted, though as I noted it may cause some times to be shown incorrectly since the tz information is stripped.

@MartinThoma
Copy link

(People coming here via Google might be interested in this stackoverflow question)

@stale
Copy link

stale bot commented Apr 10, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. For admin, please label this issue .pinned to prevent stale bot from closing the issue.

@stale stale bot added the inactive Inactive for >= 30 days label Apr 10, 2019
@stale stale bot closed this as completed Apr 17, 2019
@satvikathakur
Copy link

utc=True, format=timestamp_format

thanks this works

@davidoevans
Copy link

davidoevans commented Jul 17, 2020

utc=True, format=timestamp_format

thanks this works

@satvikathakur - were you able to specify that setting within superset when defining the table or did you have to change source code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
inactive Inactive for >= 30 days
Projects
None yet
Development

No branches or pull requests

7 participants