Skip to content

chore: remove pytz library #449

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

Merged
merged 3 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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)}"
Expand Down
4 changes: 2 additions & 2 deletions examples/import_data_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"
Expand Down
6 changes: 2 additions & 4 deletions examples/query_from_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")\
Expand Down
6 changes: 3 additions & 3 deletions influxdb_client/client/util/date_utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""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


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.

Expand Down Expand Up @@ -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:
Expand Down
6 changes: 2 additions & 4 deletions influxdb_client/client/write/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions notebooks/stock_predictions_import_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]} ' \
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
8 changes: 4 additions & 4 deletions tests/test_DateHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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__':
Expand Down
6 changes: 2 additions & 4 deletions tests/test_DeleteApi.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 3 additions & 5 deletions tests/test_PandasDateTimeHelper.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -24,8 +22,8 @@ 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')
nanoseconds = self.helper.to_nanoseconds(date - UTC.localize(datetime.utcfromtimestamp(0)))
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)

Expand Down
8 changes: 4 additions & 4 deletions tests/test_QueryApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down Expand Up @@ -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,
Expand Down
28 changes: 14 additions & 14 deletions tests/test_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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") \
Expand Down Expand Up @@ -214,15 +214,15 @@ 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)

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)
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand All @@ -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: "<class \'pytz.UTC\'>" of field: "level" is not supported.', f'{exception}')
self.assertEqual('Type: "<class \'datetime.timezone\'>" of field: "level" is not supported.', f'{exception}')

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