-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Support writing timestamps with timezones with to_sql #22654
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
Changes from 8 commits
776240b
befd200
e9f122f
969d2da
cc79b90
24dbaa5
6e86d58
c7c4a7a
6aa4878
513bbc8
58772e1
1a29148
96e9188
ded5584
d575089
a7d1b3e
305759c
7a79531
24823f8
7db4eaa
76e46dc
978a0d3
8025248
0e89370
bab5cfb
de62788
e940279
8c754b5
e85842f
5af83f7
6b3a3f1
c4304ec
1054fdb
f21c755
f872ff7
ef3b20f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -852,8 +852,13 @@ def _sqlalchemy_type(self, col): | |
if col_type == 'datetime64' or col_type == 'datetime': | ||
# GH 9086: TIMESTAMP is the suggested type if the column contains | ||
# timezone information | ||
if col.dt.tz is not None: | ||
return TIMESTAMP(timezone=True) | ||
try: | ||
if col.dt.tz is not None: | ||
return TIMESTAMP(timezone=True) | ||
except AttributeError: | ||
# The column is actually a DatetimeIndex | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just for my understanding: where was this attribute error catched before? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whoop, sorry, again misunderstanding from not looking at the full diff :-) |
||
if col.tz is not None: | ||
return TIMESTAMP(timezone=True) | ||
return DateTime | ||
if col_type == 'timedelta64': | ||
warnings.warn("the 'timedelta' type is not supported, and will be " | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1391,6 +1391,17 @@ def test_datetime_with_timezone_roundtrip(self): | |
result['A'] = to_datetime(result['A']) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_naive_datetimeindex_roundtrip(self): | ||
# GH 23510 | ||
# Ensure that a naive DatetimeIndex isn't converted to UTC | ||
dates = date_range('2018-01-01', periods=5, freq='6H') | ||
expected = DataFrame({'nums': range(5)}, index=dates) | ||
expected.to_sql('foo_table', self.conn, index_label='info_date') | ||
result = sql.read_sql_table('foo_table', self.conn, | ||
index_col='info_date') | ||
# result index with gain a name from a set_index operation; expected | ||
tm.assert_frame_equal(result, expected, check_names=False) | ||
|
||
def test_date_parsing(self): | ||
# No Parsing | ||
df = sql.read_sql_table("types_test_data", self.conn) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we rather test that it is not parsed instead of removing it? (but I agree this currently looks like this is not doing much) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see. My cursory glance thought it was duplicate of the line below; you're right, I will try to add an assert to this result as well |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would move this to the "enhancements" though, I would say that timezones were simply never supported, so it is a nice enhancement that we will now actually support it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, now looking at the full diff (and not only what changed recently), and see you actually already have that. It's a bit duplicated now, but I am fine with keeping it in both places.