Skip to content

Commit 22a8707

Browse files
authored
chore: remove pytz library (#449)
1 parent 2db3afc commit 22a8707

13 files changed

+45
-52
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
### Features
44
1. [#440](https://github.com/influxdata/influxdb-client-python/pull/440): Add possibility to specify timestamp column and its timezone [DataFrame]
55

6+
### Dependencies
7+
1. [#449](https://github.com/influxdata/influxdb-client-python/pull/449): Remove `pytz` library
8+
69
## 1.29.1 [2022-05-23]
710

811
### Bug Fixes

README.rst

+3-4
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,6 @@ The batching is configurable by ``write_options``\ :
452452
453453
import pandas as pd
454454
import rx
455-
from pytz import UTC
456455
from rx import operators as ops
457456
458457
from influxdb_client import InfluxDBClient, Point, WriteOptions
@@ -512,7 +511,7 @@ The batching is configurable by ``write_options``\ :
512511
"""
513512
Write Pandas DataFrame
514513
"""
515-
_now = datetime.now(UTC)
514+
_now = datetime.utcnow()
516515
_data_frame = pd.DataFrame(data=[["coyote_creek", 1.0], ["coyote_creek", 2.0]],
517516
index=[_now, _now + timedelta(hours=1)],
518517
columns=["location", "water_level"])
@@ -824,11 +823,11 @@ If you would like to import gigabytes of data then use our multiprocessing examp
824823
"""
825824
For better performance is sometimes useful directly create a LineProtocol to avoid unnecessary escaping overhead:
826825
"""
827-
# from pytz import UTC
826+
# from datetime import timezone
828827
# import ciso8601
829828
# from influxdb_client.client.write.point import EPOCH
830829
#
831-
# time = (UTC.localize(ciso8601.parse_datetime(row["Date"])) - EPOCH).total_seconds() * 1e9
830+
# time = (ciso8601.parse_datetime(row["Date"]).replace(tzinfo=timezone.utc) - EPOCH).total_seconds() * 1e9
832831
# return f"financial-analysis,type=vix-daily" \
833832
# f" close={float(row['VIX Close'])},high={float(row['VIX High'])},low={float(row['VIX Low'])},open={float(row['VIX Open'])} " \
834833
# f" {int(time)}"

examples/import_data_set.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ def parse_row(row: OrderedDict):
3535
"""
3636
For better performance is sometimes useful directly create a LineProtocol to avoid unnecessary escaping overhead:
3737
"""
38-
# from pytz import UTC
38+
# from datetime import timezone
3939
# import ciso8601
4040
# from influxdb_client.client.write.point import EPOCH
4141
#
42-
# time = (UTC.localize(ciso8601.parse_datetime(row["Date"])) - EPOCH).total_seconds() * 1e9
42+
# time = (ciso8601.parse_datetime(row["Date"]).replace(tzinfo=timezone.utc) - EPOCH).total_seconds() * 1e9
4343
# return f"financial-analysis,type=vix-daily" \
4444
# f" close={float(row['VIX Close'])},high={float(row['VIX High'])},low={float(row['VIX Low'])},open={float(row['VIX Open'])} " \
4545
# f" {int(time)}"

examples/query_from_file.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
"""
44
import calendar
55
import random
6-
from datetime import datetime, timedelta
7-
8-
from pytz import UTC
6+
from datetime import datetime, timedelta, timezone
97

108
from influxdb_client import InfluxDBClient, Point
119
from influxdb_client.client.write_api import SYNCHRONOUS
@@ -18,7 +16,7 @@
1816
"""
1917

2018
_points = []
21-
now = datetime.now(UTC).replace(hour=13, minute=20, second=15, microsecond=0)
19+
now = datetime.now(timezone.utc).replace(hour=13, minute=20, second=15, microsecond=0)
2220
for i in range(50):
2321
_point = Point("weather")\
2422
.tag("location", "New York")\

influxdb_client/client/util/date_utils.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
"""Utils to get right Date parsing function."""
22
import datetime
3+
from datetime import timezone as tz
34

45
from dateutil import parser
5-
from pytz import UTC
66

77
date_helper = None
88

99

1010
class DateHelper:
1111
"""DateHelper to groups different implementations of date operations."""
1212

13-
def __init__(self, timezone: datetime.tzinfo = UTC) -> None:
13+
def __init__(self, timezone: datetime.tzinfo = tz.utc) -> None:
1414
"""
1515
Initialize defaults.
1616
@@ -51,7 +51,7 @@ def to_utc(self, value: datetime):
5151
if not value.tzinfo:
5252
return self.to_utc(value.replace(tzinfo=self.timezone))
5353
else:
54-
return value.astimezone(UTC)
54+
return value.astimezone(tz.utc)
5555

5656

5757
def get_date_helper() -> DateHelper:

influxdb_client/client/write/point.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22

33
import math
44
from builtins import int
5-
from datetime import datetime, timedelta
5+
from datetime import datetime, timedelta, timezone
66
from decimal import Decimal
77
from numbers import Integral
88

9-
from pytz import UTC
10-
119
from influxdb_client.client.util.date_utils import get_date_helper
1210
from influxdb_client.domain.write_precision import WritePrecision
1311

14-
EPOCH = UTC.localize(datetime.utcfromtimestamp(0))
12+
EPOCH = datetime.utcfromtimestamp(0).replace(tzinfo=timezone.utc)
1513

1614
DEFAULT_WRITE_PRECISION = WritePrecision.NS
1715

notebooks/stock_predictions_import_data.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
"""
66
from collections import OrderedDict
77
from csv import DictReader
8+
from datetime import timezone
89

910
import ciso8601
1011
import requests
1112
import rx
12-
from pytz import UTC
1313
from rx import operators as ops
1414

1515
from influxdb_client import InfluxDBClient, WriteOptions
@@ -41,7 +41,7 @@ def parse_row(row: OrderedDict):
4141
if _progress % 10000 == 0:
4242
print(_progress)
4343

44-
time = (UTC.localize(ciso8601.parse_datetime(row["date"])) - EPOCH).total_seconds() * 1e9
44+
time = (ciso8601.parse_datetime(row["date"]).replace(tzinfo=timezone.utc) - EPOCH).total_seconds() * 1e9
4545

4646
return f'financial-analysis,symbol={row["symbol"]} ' \
4747
f'close={row["close"]},high={row["high"]},low={row["low"]},open={row["open"]} ' \

setup.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
'certifi >= 14.05.14',
1010
'python_dateutil >= 2.5.3',
1111
'setuptools >= 21.0.0',
12-
'urllib3 >= 1.26.0',
13-
'pytz>=2019.1'
12+
'urllib3 >= 1.26.0'
1413
]
1514

1615
test_requires = [

tests/test_DateHelper.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import unittest
44
from datetime import datetime, timezone
55

6-
from pytz import UTC, timezone
6+
from dateutil import tz
77

88
from influxdb_client.client.util.date_utils import DateHelper
99

@@ -12,11 +12,11 @@ class DateHelperTest(unittest.TestCase):
1212

1313
def test_to_utc(self):
1414
date = DateHelper().to_utc(datetime(2021, 4, 29, 20, 30, 10, 0))
15-
self.assertEqual(datetime(2021, 4, 29, 20, 30, 10, 0, UTC), date)
15+
self.assertEqual(datetime(2021, 4, 29, 20, 30, 10, 0, timezone.utc), date)
1616

1717
def test_to_utc_different_timezone(self):
18-
date = DateHelper(timezone=timezone('ETC/GMT+2')).to_utc(datetime(2021, 4, 29, 20, 30, 10, 0))
19-
self.assertEqual(datetime(2021, 4, 29, 22, 30, 10, 0, UTC), date)
18+
date = DateHelper(timezone=tz.gettz('ETC/GMT+2')).to_utc(datetime(2021, 4, 29, 20, 30, 10, 0))
19+
self.assertEqual(datetime(2021, 4, 29, 22, 30, 10, 0, timezone.utc), date)
2020

2121

2222
if __name__ == '__main__':

tests/test_DeleteApi.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
from datetime import datetime
2-
3-
from pytz import UTC
1+
from datetime import datetime, timezone
42

53
from influxdb_client import PermissionResource, Permission, InfluxDBClient, Point
64
from influxdb_client.client.write_api import SYNCHRONOUS
@@ -78,7 +76,7 @@ def test_delete_org_parameters_types(self):
7876
def test_start_stop_types(self):
7977
starts_stops = [
8078
("1970-01-01T00:00:00.000000001Z", "1970-01-01T00:00:00.000000012Z"),
81-
(datetime(1970, 1, 1, 0, 0, 0, 0, UTC), datetime(1970, 1, 1, 0, 0, 0, 1, UTC)),
79+
(datetime(1970, 1, 1, 0, 0, 0, 0, timezone.utc), datetime(1970, 1, 1, 0, 0, 0, 1, timezone.utc)),
8280
(datetime(1970, 1, 1, 0, 0, 0, 0), datetime(1970, 1, 1, 0, 0, 0, 1))
8381
]
8482
for start_stop in starts_stops:

tests/test_PandasDateTimeHelper.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import unittest
2-
from datetime import datetime, timedelta
3-
4-
from pytz import UTC
2+
from datetime import datetime, timedelta, timezone
53

64
from influxdb_client.client.util.date_utils_pandas import PandasDateTimeHelper
75

@@ -24,8 +22,8 @@ def test_parse_date(self):
2422
self.assertEqual(date.nanosecond, 158)
2523

2624
def test_to_nanoseconds(self):
27-
date = self.helper.parse_date('2020-08-07T06:21:57.331249158Z')
28-
nanoseconds = self.helper.to_nanoseconds(date - UTC.localize(datetime.utcfromtimestamp(0)))
25+
date = self.helper.parse_date('2020-08-07T06:21:57.331249158Z').replace(tzinfo=timezone.utc)
26+
nanoseconds = self.helper.to_nanoseconds(date - datetime.utcfromtimestamp(0).replace(tzinfo=timezone.utc))
2927

3028
self.assertEqual(nanoseconds, 1596781317331249158)
3129

tests/test_QueryApi.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import json
33
import unittest
44

5-
from dateutil.tz import tzutc
5+
from datetime import timezone
66
from httpretty import httpretty
77

88
from influxdb_client import QueryApi, DurationLiteral, Duration, CallExpression, UnaryExpression, \
@@ -506,9 +506,9 @@ def test_profiler_mock(self):
506506

507507
self.assertEqual(tables[0].records[5].values,
508508
{'result': '_result', 'table': 0,
509-
'_start': datetime.datetime(2021, 5, 24, 8, 40, 44, 785000, tzinfo=tzutc()),
510-
'_stop': datetime.datetime(2021, 5, 24, 8, 45, 44, 785000, tzinfo=tzutc()),
511-
'_time': datetime.datetime(2021, 5, 24, 8, 45, 44, 785000, tzinfo=tzutc()),
509+
'_start': datetime.datetime(2021, 5, 24, 8, 40, 44, 785000, tzinfo=timezone.utc),
510+
'_stop': datetime.datetime(2021, 5, 24, 8, 45, 44, 785000, tzinfo=timezone.utc),
511+
'_time': datetime.datetime(2021, 5, 24, 8, 45, 44, 785000, tzinfo=timezone.utc),
512512
'_measurement': 'mem',
513513
'host': 'kozel.local',
514514
'available': 5727718400, 'free': 35330048,

tests/test_point.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from datetime import datetime, timezone, timedelta
55
from decimal import Decimal
66

7-
from pytz import UTC, timezone
7+
from dateutil import tz
88

99
from influxdb_client import Point, WritePrecision
1010

@@ -181,7 +181,7 @@ def test_DateTimeFormatting(self):
181181

182182
self.assertEqual("h2o,location=europe level=2i 1444897215000", point.to_line_protocol())
183183

184-
date_time = datetime(2015, 10, 15, 8, 20, 15, 750, UTC)
184+
date_time = datetime(2015, 10, 15, 8, 20, 15, 750, timezone.utc)
185185

186186
point = Point.measurement("h2o") \
187187
.tag("location", "europe") \
@@ -214,15 +214,15 @@ def test_DateTimeFormatting(self):
214214
point = Point.measurement("h2o") \
215215
.tag("location", "europe") \
216216
.field("level", True) \
217-
.time(datetime.now(UTC), WritePrecision.S)
217+
.time(datetime.now(timezone.utc), WritePrecision.S)
218218

219219
line_protocol = point.to_line_protocol()
220220
self.assertTrue("." not in line_protocol)
221221

222222
point = Point.measurement("h2o") \
223223
.tag("location", "europe") \
224224
.field("level", True) \
225-
.time(datetime.now(UTC), WritePrecision.NS)
225+
.time(datetime.now(timezone.utc), WritePrecision.NS)
226226

227227
line_protocol = point.to_line_protocol()
228228
self.assertTrue("." not in line_protocol)
@@ -293,9 +293,9 @@ def test_lineprotocol_encode(self):
293293
def test_timestamp(self):
294294
"""Test timezone in TestLineProtocol object."""
295295
dt = datetime(2009, 11, 10, 23, 0, 0, 123456)
296-
utc = UTC.localize(dt)
297-
berlin = timezone('Europe/Berlin').localize(dt)
298-
eastern = berlin.astimezone(timezone('US/Eastern'))
296+
utc = dt.replace(tzinfo=timezone.utc)
297+
berlin = dt.replace(tzinfo=tz.gettz('Europe/Berlin'))
298+
eastern = berlin.astimezone(tz.gettz('US/Eastern'))
299299

300300
exp_utc = 'A val=1i 1257894000123456000'
301301
exp_est = 'A val=1i 1257890400123456000'
@@ -335,9 +335,9 @@ def test_only_infinity_values(self):
335335
def test_timezone(self):
336336
"""Test timezone in TestLineProtocol object."""
337337
dt = datetime(2009, 11, 10, 23, 0, 0, 123456)
338-
utc = UTC.localize(dt)
339-
berlin = timezone('Europe/Berlin').localize(dt)
340-
eastern = berlin.astimezone(timezone('US/Eastern'))
338+
utc = dt.replace(tzinfo=timezone.utc)
339+
berlin = dt.replace(tzinfo=tz.gettz('Europe/Berlin'))
340+
eastern = berlin.astimezone(tz.gettz('US/Eastern'))
341341

342342
self.assertEqual("h2o val=1i 0", Point.measurement("h2o").field("val", 1).time(0).to_line_protocol())
343343
self.assertEqual("h2o val=1i 1257894000123456000", Point.measurement("h2o").field("val", 1).time(
@@ -367,8 +367,8 @@ def test_from_dict_without_tags(self):
367367
self.assertEqual("my-org field1=1i,field2=2i", point.to_line_protocol())
368368

369369
def test_points_from_different_timezones(self):
370-
time_in_utc = UTC.localize(datetime(2020, 7, 4, 0, 0, 0, 123456))
371-
time_in_hk = timezone('Asia/Hong_Kong').localize(datetime(2020, 7, 4, 8, 0, 0, 123456)) # +08:00
370+
time_in_utc = datetime(2020, 7, 4, 0, 0, 0, 123456).replace(tzinfo=timezone.utc)
371+
time_in_hk = datetime(2020, 7, 4, 8, 0, 0, 123456).replace(tzinfo=tz.gettz('Asia/Hong_Kong')) # +08:00
372372

373373
point_utc = Point.measurement("h2o").field("val", 1).time(time_in_utc)
374374
point_hk = Point.measurement("h2o").field("val", 1).time(time_in_hk)
@@ -378,11 +378,11 @@ def test_unsupported_field_type(self):
378378
with self.assertRaises(ValueError) as ve:
379379
Point.measurement("h2o") \
380380
.tag("location", "europe") \
381-
.field("level", UTC) \
381+
.field("level", timezone.utc) \
382382
.to_line_protocol()
383383
exception = ve.exception
384384

385-
self.assertEqual('Type: "<class \'pytz.UTC\'>" of field: "level" is not supported.', f'{exception}')
385+
self.assertEqual('Type: "<class \'datetime.timezone\'>" of field: "level" is not supported.', f'{exception}')
386386

387387
def test_backslash(self):
388388
point = Point.from_dict({"measurement": "test",

0 commit comments

Comments
 (0)