From 747d5fc863c115d7a38d0bdf2d5f227f65c06f9d Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Tue, 31 May 2022 08:20:42 +0200 Subject: [PATCH 1/3] chore: remove `pytz` library --- README.rst | 7 +++--- examples/import_data_set.py | 4 ++-- examples/query_from_file.py | 6 ++--- influxdb_client/client/util/date_utils.py | 6 ++--- influxdb_client/client/write/point.py | 6 ++--- notebooks/stock_predictions_import_data.py | 4 ++-- setup.py | 3 +-- tests/test_DateHelper.py | 8 +++---- tests/test_DeleteApi.py | 6 ++--- tests/test_PandasDateTimeHelper.py | 6 ++--- tests/test_QueryApi.py | 8 +++---- tests/test_point.py | 28 +++++++++++----------- 12 files changed, 41 insertions(+), 51 deletions(-) diff --git a/README.rst b/README.rst index d0c499da..94603bd6 100644 --- a/README.rst +++ b/README.rst @@ -452,7 +452,6 @@ The batching is configurable by ``write_options``\ : import pandas as pd import rx - from pytz import UTC from rx import operators as ops from influxdb_client import InfluxDBClient, Point, WriteOptions @@ -512,7 +511,7 @@ The batching is configurable by ``write_options``\ : """ Write Pandas DataFrame """ - _now = datetime.now(UTC) + _now = datetime.utcnow() _data_frame = pd.DataFrame(data=[["coyote_creek", 1.0], ["coyote_creek", 2.0]], index=[_now, _now + timedelta(hours=1)], columns=["location", "water_level"]) @@ -824,11 +823,11 @@ If you would like to import gigabytes of data then use our multiprocessing examp """ For better performance is sometimes useful directly create a LineProtocol to avoid unnecessary escaping overhead: """ - # from pytz import UTC + # from datetime import timezone # import ciso8601 # from influxdb_client.client.write.point import EPOCH # - # time = (UTC.localize(ciso8601.parse_datetime(row["Date"])) - EPOCH).total_seconds() * 1e9 + # time = (ciso8601.parse_datetime(row["Date"]).replace(tzinfo=timezone.utc) - EPOCH).total_seconds() * 1e9 # return f"financial-analysis,type=vix-daily" \ # f" close={float(row['VIX Close'])},high={float(row['VIX High'])},low={float(row['VIX Low'])},open={float(row['VIX Open'])} " \ # f" {int(time)}" diff --git a/examples/import_data_set.py b/examples/import_data_set.py index 0777f82c..8ddcef1b 100644 --- a/examples/import_data_set.py +++ b/examples/import_data_set.py @@ -35,11 +35,11 @@ def parse_row(row: OrderedDict): """ For better performance is sometimes useful directly create a LineProtocol to avoid unnecessary escaping overhead: """ - # from pytz import UTC + # from datetime import timezone # import ciso8601 # from influxdb_client.client.write.point import EPOCH # - # time = (UTC.localize(ciso8601.parse_datetime(row["Date"])) - EPOCH).total_seconds() * 1e9 + # time = (ciso8601.parse_datetime(row["Date"]).replace(tzinfo=timezone.utc) - EPOCH).total_seconds() * 1e9 # return f"financial-analysis,type=vix-daily" \ # f" close={float(row['VIX Close'])},high={float(row['VIX High'])},low={float(row['VIX Low'])},open={float(row['VIX Open'])} " \ # f" {int(time)}" diff --git a/examples/query_from_file.py b/examples/query_from_file.py index d1a545de..5ed4f0ad 100644 --- a/examples/query_from_file.py +++ b/examples/query_from_file.py @@ -3,9 +3,7 @@ """ import calendar import random -from datetime import datetime, timedelta - -from pytz import UTC +from datetime import datetime, timedelta, timezone from influxdb_client import InfluxDBClient, Point from influxdb_client.client.write_api import SYNCHRONOUS @@ -18,7 +16,7 @@ """ _points = [] - now = datetime.now(UTC).replace(hour=13, minute=20, second=15, microsecond=0) + now = datetime.now(timezone.utc).replace(hour=13, minute=20, second=15, microsecond=0) for i in range(50): _point = Point("weather")\ .tag("location", "New York")\ diff --git a/influxdb_client/client/util/date_utils.py b/influxdb_client/client/util/date_utils.py index f1f6f39f..22de0ad0 100644 --- a/influxdb_client/client/util/date_utils.py +++ b/influxdb_client/client/util/date_utils.py @@ -1,8 +1,8 @@ """Utils to get right Date parsing function.""" import datetime +from datetime import timezone as tz from dateutil import parser -from pytz import UTC date_helper = None @@ -10,7 +10,7 @@ class DateHelper: """DateHelper to groups different implementations of date operations.""" - def __init__(self, timezone: datetime.tzinfo = UTC) -> None: + def __init__(self, timezone: datetime.tzinfo = tz.utc) -> None: """ Initialize defaults. @@ -51,7 +51,7 @@ def to_utc(self, value: datetime): if not value.tzinfo: return self.to_utc(value.replace(tzinfo=self.timezone)) else: - return value.astimezone(UTC) + return value.astimezone(tz.utc) def get_date_helper() -> DateHelper: diff --git a/influxdb_client/client/write/point.py b/influxdb_client/client/write/point.py index a0517cb7..3b9e5439 100644 --- a/influxdb_client/client/write/point.py +++ b/influxdb_client/client/write/point.py @@ -2,16 +2,14 @@ import math from builtins import int -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from decimal import Decimal from numbers import Integral -from pytz import UTC - from influxdb_client.client.util.date_utils import get_date_helper from influxdb_client.domain.write_precision import WritePrecision -EPOCH = UTC.localize(datetime.utcfromtimestamp(0)) +EPOCH = datetime.utcfromtimestamp(0).replace(tzinfo=timezone.utc) DEFAULT_WRITE_PRECISION = WritePrecision.NS diff --git a/notebooks/stock_predictions_import_data.py b/notebooks/stock_predictions_import_data.py index 4c479911..2d15c87c 100644 --- a/notebooks/stock_predictions_import_data.py +++ b/notebooks/stock_predictions_import_data.py @@ -5,11 +5,11 @@ """ from collections import OrderedDict from csv import DictReader +from datetime import timezone import ciso8601 import requests import rx -from pytz import UTC from rx import operators as ops from influxdb_client import InfluxDBClient, WriteOptions @@ -41,7 +41,7 @@ def parse_row(row: OrderedDict): if _progress % 10000 == 0: print(_progress) - time = (UTC.localize(ciso8601.parse_datetime(row["date"])) - EPOCH).total_seconds() * 1e9 + time = (ciso8601.parse_datetime(row["date"]).replace(tzinfo=timezone.utc) - EPOCH).total_seconds() * 1e9 return f'financial-analysis,symbol={row["symbol"]} ' \ f'close={row["close"]},high={row["high"]},low={row["low"]},open={row["open"]} ' \ diff --git a/setup.py b/setup.py index 93703cac..1eae7d66 100644 --- a/setup.py +++ b/setup.py @@ -9,8 +9,7 @@ 'certifi >= 14.05.14', 'python_dateutil >= 2.5.3', 'setuptools >= 21.0.0', - 'urllib3 >= 1.26.0', - 'pytz>=2019.1' + 'urllib3 >= 1.26.0' ] test_requires = [ diff --git a/tests/test_DateHelper.py b/tests/test_DateHelper.py index abff4e56..368561e4 100644 --- a/tests/test_DateHelper.py +++ b/tests/test_DateHelper.py @@ -3,7 +3,7 @@ import unittest from datetime import datetime, timezone -from pytz import UTC, timezone +from dateutil import tz from influxdb_client.client.util.date_utils import DateHelper @@ -12,11 +12,11 @@ class DateHelperTest(unittest.TestCase): def test_to_utc(self): date = DateHelper().to_utc(datetime(2021, 4, 29, 20, 30, 10, 0)) - self.assertEqual(datetime(2021, 4, 29, 20, 30, 10, 0, UTC), date) + self.assertEqual(datetime(2021, 4, 29, 20, 30, 10, 0, timezone.utc), date) def test_to_utc_different_timezone(self): - date = DateHelper(timezone=timezone('ETC/GMT+2')).to_utc(datetime(2021, 4, 29, 20, 30, 10, 0)) - self.assertEqual(datetime(2021, 4, 29, 22, 30, 10, 0, UTC), date) + date = DateHelper(timezone=tz.gettz('ETC/GMT+2')).to_utc(datetime(2021, 4, 29, 20, 30, 10, 0)) + self.assertEqual(datetime(2021, 4, 29, 22, 30, 10, 0, timezone.utc), date) if __name__ == '__main__': diff --git a/tests/test_DeleteApi.py b/tests/test_DeleteApi.py index b0ac6abd..b88f9354 100644 --- a/tests/test_DeleteApi.py +++ b/tests/test_DeleteApi.py @@ -1,6 +1,4 @@ -from datetime import datetime - -from pytz import UTC +from datetime import datetime, timezone from influxdb_client import PermissionResource, Permission, InfluxDBClient, Point from influxdb_client.client.write_api import SYNCHRONOUS @@ -78,7 +76,7 @@ def test_delete_org_parameters_types(self): def test_start_stop_types(self): starts_stops = [ ("1970-01-01T00:00:00.000000001Z", "1970-01-01T00:00:00.000000012Z"), - (datetime(1970, 1, 1, 0, 0, 0, 0, UTC), datetime(1970, 1, 1, 0, 0, 0, 1, UTC)), + (datetime(1970, 1, 1, 0, 0, 0, 0, timezone.utc), datetime(1970, 1, 1, 0, 0, 0, 1, timezone.utc)), (datetime(1970, 1, 1, 0, 0, 0, 0), datetime(1970, 1, 1, 0, 0, 0, 1)) ] for start_stop in starts_stops: diff --git a/tests/test_PandasDateTimeHelper.py b/tests/test_PandasDateTimeHelper.py index c77f7163..58961968 100644 --- a/tests/test_PandasDateTimeHelper.py +++ b/tests/test_PandasDateTimeHelper.py @@ -1,7 +1,5 @@ import unittest -from datetime import datetime, timedelta - -from pytz import UTC +from datetime import datetime, timedelta, timezone from influxdb_client.client.util.date_utils_pandas import PandasDateTimeHelper @@ -25,7 +23,7 @@ def test_parse_date(self): def test_to_nanoseconds(self): date = self.helper.parse_date('2020-08-07T06:21:57.331249158Z') - nanoseconds = self.helper.to_nanoseconds(date - UTC.localize(datetime.utcfromtimestamp(0))) + nanoseconds = self.helper.to_nanoseconds(date - datetime.utcfromtimestamp(0).replace(tzinfo=timezone.utc)) self.assertEqual(nanoseconds, 1596781317331249158) diff --git a/tests/test_QueryApi.py b/tests/test_QueryApi.py index bda2b068..7e5e7241 100644 --- a/tests/test_QueryApi.py +++ b/tests/test_QueryApi.py @@ -2,7 +2,7 @@ import json import unittest -from dateutil.tz import tzutc +from datetime import timezone from httpretty import httpretty from influxdb_client import QueryApi, DurationLiteral, Duration, CallExpression, UnaryExpression, \ @@ -506,9 +506,9 @@ def test_profiler_mock(self): self.assertEqual(tables[0].records[5].values, {'result': '_result', 'table': 0, - '_start': datetime.datetime(2021, 5, 24, 8, 40, 44, 785000, tzinfo=tzutc()), - '_stop': datetime.datetime(2021, 5, 24, 8, 45, 44, 785000, tzinfo=tzutc()), - '_time': datetime.datetime(2021, 5, 24, 8, 45, 44, 785000, tzinfo=tzutc()), + '_start': datetime.datetime(2021, 5, 24, 8, 40, 44, 785000, tzinfo=timezone.utc), + '_stop': datetime.datetime(2021, 5, 24, 8, 45, 44, 785000, tzinfo=timezone.utc), + '_time': datetime.datetime(2021, 5, 24, 8, 45, 44, 785000, tzinfo=timezone.utc), '_measurement': 'mem', 'host': 'kozel.local', 'available': 5727718400, 'free': 35330048, diff --git a/tests/test_point.py b/tests/test_point.py index 00528c7a..4b08c6cb 100644 --- a/tests/test_point.py +++ b/tests/test_point.py @@ -4,7 +4,7 @@ from datetime import datetime, timezone, timedelta from decimal import Decimal -from pytz import UTC, timezone +from dateutil import tz from influxdb_client import Point, WritePrecision @@ -181,7 +181,7 @@ def test_DateTimeFormatting(self): self.assertEqual("h2o,location=europe level=2i 1444897215000", point.to_line_protocol()) - date_time = datetime(2015, 10, 15, 8, 20, 15, 750, UTC) + date_time = datetime(2015, 10, 15, 8, 20, 15, 750, timezone.utc) point = Point.measurement("h2o") \ .tag("location", "europe") \ @@ -214,7 +214,7 @@ def test_DateTimeFormatting(self): point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", True) \ - .time(datetime.now(UTC), WritePrecision.S) + .time(datetime.now(timezone.utc), WritePrecision.S) line_protocol = point.to_line_protocol() self.assertTrue("." not in line_protocol) @@ -222,7 +222,7 @@ def test_DateTimeFormatting(self): point = Point.measurement("h2o") \ .tag("location", "europe") \ .field("level", True) \ - .time(datetime.now(UTC), WritePrecision.NS) + .time(datetime.now(timezone.utc), WritePrecision.NS) line_protocol = point.to_line_protocol() self.assertTrue("." not in line_protocol) @@ -293,9 +293,9 @@ def test_lineprotocol_encode(self): def test_timestamp(self): """Test timezone in TestLineProtocol object.""" dt = datetime(2009, 11, 10, 23, 0, 0, 123456) - utc = UTC.localize(dt) - berlin = timezone('Europe/Berlin').localize(dt) - eastern = berlin.astimezone(timezone('US/Eastern')) + utc = dt.replace(tzinfo=timezone.utc) + berlin = dt.replace(tzinfo=tz.gettz('Europe/Berlin')) + eastern = berlin.astimezone(tz.gettz('US/Eastern')) exp_utc = 'A val=1i 1257894000123456000' exp_est = 'A val=1i 1257890400123456000' @@ -335,9 +335,9 @@ def test_only_infinity_values(self): def test_timezone(self): """Test timezone in TestLineProtocol object.""" dt = datetime(2009, 11, 10, 23, 0, 0, 123456) - utc = UTC.localize(dt) - berlin = timezone('Europe/Berlin').localize(dt) - eastern = berlin.astimezone(timezone('US/Eastern')) + utc = dt.replace(tzinfo=timezone.utc) + berlin = dt.replace(tzinfo=tz.gettz('Europe/Berlin')) + eastern = berlin.astimezone(tz.gettz('US/Eastern')) self.assertEqual("h2o val=1i 0", Point.measurement("h2o").field("val", 1).time(0).to_line_protocol()) self.assertEqual("h2o val=1i 1257894000123456000", Point.measurement("h2o").field("val", 1).time( @@ -367,8 +367,8 @@ def test_from_dict_without_tags(self): self.assertEqual("my-org field1=1i,field2=2i", point.to_line_protocol()) def test_points_from_different_timezones(self): - time_in_utc = UTC.localize(datetime(2020, 7, 4, 0, 0, 0, 123456)) - time_in_hk = timezone('Asia/Hong_Kong').localize(datetime(2020, 7, 4, 8, 0, 0, 123456)) # +08:00 + time_in_utc = datetime(2020, 7, 4, 0, 0, 0, 123456).replace(tzinfo=timezone.utc) + time_in_hk = datetime(2020, 7, 4, 8, 0, 0, 123456).replace(tzinfo=tz.gettz('Asia/Hong_Kong')) # +08:00 point_utc = Point.measurement("h2o").field("val", 1).time(time_in_utc) point_hk = Point.measurement("h2o").field("val", 1).time(time_in_hk) @@ -378,11 +378,11 @@ def test_unsupported_field_type(self): with self.assertRaises(ValueError) as ve: Point.measurement("h2o") \ .tag("location", "europe") \ - .field("level", UTC) \ + .field("level", timezone.utc) \ .to_line_protocol() exception = ve.exception - self.assertEqual('Type: "" of field: "level" is not supported.', f'{exception}') + self.assertEqual('Type: "" of field: "level" is not supported.', f'{exception}') def test_backslash(self): point = Point.from_dict({"measurement": "test", From 8338f269e31f8d76bcd734abd0e60e266d3afc97 Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Tue, 31 May 2022 08:25:50 +0200 Subject: [PATCH 2/3] fix: test --- tests/test_PandasDateTimeHelper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_PandasDateTimeHelper.py b/tests/test_PandasDateTimeHelper.py index 58961968..60017172 100644 --- a/tests/test_PandasDateTimeHelper.py +++ b/tests/test_PandasDateTimeHelper.py @@ -22,7 +22,7 @@ def test_parse_date(self): self.assertEqual(date.nanosecond, 158) def test_to_nanoseconds(self): - date = self.helper.parse_date('2020-08-07T06:21:57.331249158Z') + date = self.helper.parse_date('2020-08-07T06:21:57.331249158Z').replace(tzinfo=timezone.utc) nanoseconds = self.helper.to_nanoseconds(date - datetime.utcfromtimestamp(0).replace(tzinfo=timezone.utc)) self.assertEqual(nanoseconds, 1596781317331249158) From c6c364baaa9e09b11bb37e21d63986f3128d44dc Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Tue, 31 May 2022 08:35:24 +0200 Subject: [PATCH 3/3] docs: update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8fea709..e7500098 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ ### Features 1. [#440](https://github.com/influxdata/influxdb-client-python/pull/440): Add possibility to specify timestamp column and its timezone [DataFrame] +### Dependencies +1. [#449](https://github.com/influxdata/influxdb-client-python/pull/449): Remove `pytz` library + ## 1.29.1 [2022-05-23] ### Bug Fixes