Skip to content

Commit

Permalink
Do not localize already localized pandas datetime #444
Browse files Browse the repository at this point in the history
  • Loading branch information
xzkostyan committed Aug 5, 2024
1 parent a993d27 commit 82229c3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion clickhouse_driver/columns/numpy/datetimecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def apply_timezones_before_write(self, items):
ts = items
else:
timezone = self.timezone if self.timezone else self.local_timezone
ts = pd.to_datetime(items).tz_localize(timezone)
ts = pd.to_datetime(items)
if not getattr(ts.dtype, 'tz', None):
ts = ts.tz_localize(timezone)

ts = ts.tz_convert('UTC')
return ts.tz_localize(None).to_numpy(self.datetime_dtype)
Expand Down
23 changes: 23 additions & 0 deletions tests/numpy/columns/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,29 @@ def test_read_tz_naive_column_with_client_timezone(self):
self.make_numpy_d64ns([self.dt_str] * 2)
)

@require_server_version(1, 1, 54337)
def test_do_not_localize_already_localized(self):
# Insert datetime with timezone Asia/Kamchatka
# into column with no timezone
# using server's timezone (Europe/Moscow)
with patch_env_tz('Asia/Novosibirsk'):
with self.create_table(self.table_columns()):
self.client.execute(
'INSERT INTO test (a) VALUES',
[np.array([pd.Timestamp('2017-07-14 05:40:00+1200')])],
columnar=True
)

self.emit_cli(
"INSERT INTO test (a) VALUES "
"(toDateTime('2017-07-14 05:40:00', 'Asia/Kamchatka'))",
)

query = 'SELECT toInt32(a) FROM test'
inserted = self.emit_cli(query)
# 1499967600 = 1500000000 - 12 * 3600
self.assertEqual(inserted, '1499967600\n1499967600\n')


class DateTime64TimezonesTestCase(DateTimeTimezonesTestCase):
dt_type = 'DateTime64'
Expand Down

0 comments on commit 82229c3

Please sign in to comment.