-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added possibility to use datetime nanoseconds precision by `pan…
…das.Timestamp` (#141)
- Loading branch information
Showing
11 changed files
with
261 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from influxdb_client import Point, InfluxDBClient | ||
from influxdb_client.client.util.date_utils_pandas import PandasDateTimeHelper | ||
from influxdb_client.client.write_api import SYNCHRONOUS | ||
|
||
""" | ||
Set PandasDate helper which supports nanoseconds. | ||
""" | ||
import influxdb_client.client.util.date_utils as date_utils | ||
|
||
date_utils.date_helper = PandasDateTimeHelper() | ||
|
||
""" | ||
Prepare client. | ||
""" | ||
client = InfluxDBClient(url="http://localhost:9999", token="my-token", org="my-org") | ||
|
||
write_api = client.write_api(write_options=SYNCHRONOUS) | ||
query_api = client.query_api() | ||
|
||
""" | ||
Prepare data | ||
""" | ||
|
||
point = Point("h2o_feet") \ | ||
.field("water_level", 10) \ | ||
.tag("location", "pacific") \ | ||
.time('1996-02-25T21:20:00.001001231Z') | ||
|
||
print(f'Time serialized with nanosecond precision: {point.to_line_protocol()}') | ||
print() | ||
|
||
write_api.write(bucket="my-bucket", record=point) | ||
|
||
""" | ||
Query: using Stream | ||
""" | ||
query = ''' | ||
from(bucket:"my-bucket") | ||
|> range(start: 0, stop: now()) | ||
|> filter(fn: (r) => r._measurement == "h2o_feet") | ||
''' | ||
records = query_api.query_stream(query) | ||
|
||
for record in records: | ||
print(f'Temperature in {record["location"]} is {record["_value"]} at time: {record["_time"]}') | ||
|
||
""" | ||
Close client | ||
""" | ||
client.__del__() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Utils package.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""Utils to get right Date parsing function.""" | ||
|
||
from dateutil import parser | ||
|
||
date_helper = None | ||
|
||
|
||
class DateHelper: | ||
"""DateHelper to groups different implementations of date operations.""" | ||
|
||
def parse_date(self, date_string: str): | ||
""" | ||
Parse string into Date or Timestamp. | ||
:return: Returns a :class:`datetime.datetime` object or compliant implementation | ||
like :class:`class 'pandas._libs.tslibs.timestamps.Timestamp` | ||
""" | ||
pass | ||
|
||
def to_nanoseconds(self, delta): | ||
""" | ||
Get number of nanoseconds in timedelta. | ||
Solution comes from v1 client. Thx. | ||
https://github.com/influxdata/influxdb-python/pull/811 | ||
""" | ||
nanoseconds_in_days = delta.days * 86400 * 10 ** 9 | ||
nanoseconds_in_seconds = delta.seconds * 10 ** 9 | ||
nanoseconds_in_micros = delta.microseconds * 10 ** 3 | ||
|
||
return nanoseconds_in_days + nanoseconds_in_seconds + nanoseconds_in_micros | ||
|
||
|
||
def get_date_helper() -> DateHelper: | ||
""" | ||
Return DateHelper with proper implementation. | ||
If there is a 'ciso8601' than use 'ciso8601.parse_datetime' else use 'dateutil.parse'. | ||
""" | ||
global date_helper | ||
if date_helper is None: | ||
date_helper = DateHelper() | ||
try: | ||
import ciso8601 | ||
date_helper.parse_date = ciso8601.parse_datetime | ||
except ModuleNotFoundError: | ||
date_helper.parse_date = parser.parse | ||
|
||
return date_helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
"""Pandas date utils.""" | ||
from influxdb_client.client.util.date_utils import DateHelper | ||
from influxdb_client.extras import pd | ||
|
||
|
||
class PandasDateTimeHelper(DateHelper): | ||
"""DateHelper that use Pandas library with nanosecond precision.""" | ||
|
||
def parse_date(self, date_string: str): | ||
"""Parse date string into `class 'pandas._libs.tslibs.timestamps.Timestamp`.""" | ||
return pd.to_datetime(date_string) | ||
|
||
def to_nanoseconds(self, delta): | ||
"""Get number of nanoseconds with nanos precision.""" | ||
return super().to_nanoseconds(delta) + (delta.nanoseconds if hasattr(delta, 'nanoseconds') else 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import unittest | ||
from datetime import datetime, timedelta | ||
|
||
from pytz import UTC | ||
|
||
from influxdb_client.client.util.date_utils_pandas import PandasDateTimeHelper | ||
|
||
|
||
class PandasDateTimeHelperTest(unittest.TestCase): | ||
|
||
def setUp(self) -> None: | ||
self.helper = PandasDateTimeHelper() | ||
|
||
def test_parse_date(self): | ||
date = self.helper.parse_date('2020-08-07T06:21:57.331249158Z') | ||
|
||
self.assertEqual(date.year, 2020) | ||
self.assertEqual(date.month, 8) | ||
self.assertEqual(date.day, 7) | ||
self.assertEqual(date.hour, 6) | ||
self.assertEqual(date.minute, 21) | ||
self.assertEqual(date.second, 57) | ||
self.assertEqual(date.microsecond, 331249) | ||
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))) | ||
|
||
self.assertEqual(nanoseconds, 1596781317331249158) | ||
|
||
def test_to_nanoseconds_buildin_timedelta(self): | ||
nanoseconds = self.helper.to_nanoseconds(timedelta(days=1)) | ||
|
||
self.assertEqual(nanoseconds, 86400000000000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters