Skip to content
Open
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
2 changes: 1 addition & 1 deletion conda/dev-environment-unix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ dependencies:
- python-build
- python-graphviz
- python-rapidjson
- pytz
- python-tzdata
- rapidjson
- requests
- ruamel.yaml
Expand Down
2 changes: 1 addition & 1 deletion conda/dev-environment-win.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ dependencies:
- python-build
- python-graphviz
- python-rapidjson
- pytz
- python-tzdata
- rapidjson
- requests
- ruamel.yaml
Expand Down
2 changes: 1 addition & 1 deletion csp/adapters/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def convert(row):
v = row[column]
dt = datetime.strptime(v, format_string)
if tz is not None:
dt = tz.localize(dt)
dt = tz.localize(dt) if hasattr(tz, "localize") else dt.replace(tzinfo=tz)
return dt

return convert
Expand Down
5 changes: 2 additions & 3 deletions csp/adapters/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from importlib.metadata import PackageNotFoundError, version as get_package_version

import pytz
from packaging import version

from csp import PushMode, ts
Expand Down Expand Up @@ -74,7 +73,7 @@ def __init__(self, time_column, tz=None):
:param tz: timezone if the db timestamp doesn't have timezone
"""
self._time_column = time_column
if isinstance(tz, pytz.BaseTzInfo):
if getattr(tz, "zone", None):
tz = zoneinfo.ZoneInfo(tz.zone)

self._tz = tz
Expand Down Expand Up @@ -122,7 +121,7 @@ def __init__(self, date_column, time_column, tz=None):
self._date_column = date_column
self._time_column = time_column

if isinstance(tz, pytz.BaseTzInfo):
if getattr(tz, "zone", None):
tz = zoneinfo.ZoneInfo(tz.zone)
self._tz = tz
self._db_has_tz = None
Expand Down
4 changes: 2 additions & 2 deletions csp/adapters/parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def __init__(
:param symbol_column: An optional parameter that specifies the name of the symbol column if the file if there is any
:param time_column: A mandatory specification of the time column name in the parquet files. This column will be used to inject the row values
from parquet at the given timestamps.
:param tz: The pytz timezone of the timestamp column, should only be provided if the time_column in parquet file doesn't have tz info.
:param tz: The timezone (e.g. zoneinfo.ZoneInfo) of the timestamp column, should only be provided if the time_column in parquet file doesn't have tz info.
:param start_time: An optional start time of the date, any data prior to this time will be ignored
:param end_time: An optional end time of the data, any data after this time will be ignored
:param split_columns_to_files: A boolean flag that specifies that each column should be written to a separate file. If true, then
Expand Down Expand Up @@ -166,7 +166,7 @@ def __init__(
if time_column:
self._properties["time_column"] = time_column
if tz:
self._properties["tz"] = tz.zone
self._properties["tz"] = str(tz)
if binary_arrow:
self._properties["is_arrow_ipc"] = True
if start_time:
Expand Down
7 changes: 3 additions & 4 deletions csp/baselib.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
import math
import queue
import threading
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from typing import Callable, Dict, List, Optional, TypeVar, Union

import numpy as np
import pytz

import csp
from csp.impl.__cspimpl import _cspimpl
Expand Down Expand Up @@ -186,7 +185,7 @@ def _print_ts(tag: str, x: ts["T"]):
if csp.ticked(x):
t = csp.now()
if s_log_tz is not None:
t = pytz.UTC.localize(t).astimezone(s_log_tz)
t = t.replace(tzinfo=timezone.utc).astimezone(s_log_tz)
builtins.print("%s %s:%s" % (t, tag, x))


Expand Down Expand Up @@ -266,7 +265,7 @@ def _log_ts(
if csp.ticked(x):
t = csp.now()
if s_log_tz is not None:
t = pytz.UTC.localize(t).astimezone(s_log_tz)
t = t.replace(tzinfo=timezone.utc).astimezone(s_log_tz)

if use_thread:
s_queue.put((s_logger, level, tag, t, x))
Expand Down
5 changes: 2 additions & 3 deletions csp/curve.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import copy
from datetime import timedelta
from datetime import timedelta, timezone
from typing import Union

import numpy as np
import pytz

from csp import null_ts
from csp.impl.__cspimpl import _cspimpl
Expand All @@ -27,7 +26,7 @@ def start(self, start_time, end_time):
self._data[idx] = (start_time + data[0], data[1])
elif self._data[0][0].tzinfo is not None:
for idx, data in enumerate(self._data):
self._data[idx] = (data[0].astimezone(pytz.UTC).replace(tzinfo=None), data[1])
self._data[idx] = (data[0].astimezone(timezone.utc).replace(tzinfo=None), data[1])

while self._index < len(self._data) and self._data[self._index][0] < start_time:
self._index += 1
Expand Down
7 changes: 3 additions & 4 deletions csp/impl/pandas_perspective.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from typing import Optional

import pandas as pd
import pytz
from pandas.compat import set_function_name

import csp
Expand Down Expand Up @@ -66,7 +65,7 @@ def _apply_updates(
row[index_col] = idx
if s_has_time_col:
if localize:
row[time_col] = pytz.utc.localize(csp.now())
row[time_col] = csp.now().replace(tzinfo=timezone.utc)
else:
row[time_col] = csp.now()
if _PERSPECTIVE_3:
Expand All @@ -75,7 +74,7 @@ def _apply_updates(
row = new_rows[idx]

if localize and col in s_datetime_cols and value.tzinfo is None:
row[col] = pytz.utc.localize(value)
row[col] = value.replace(tzinfo=timezone.utc)
else:
row[col] = value

Expand Down
7 changes: 3 additions & 4 deletions csp/impl/perspective_common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from datetime import date, datetime
from datetime import date, datetime, timezone

import pytz
from packaging import version

try:
Expand Down Expand Up @@ -43,10 +42,10 @@ def perspective_type_map():
def datetime_to_perspective(dt: datetime) -> int:
"""Convert a python datetime to an integer number of milliseconds for perspective >= 3"""
if dt.tzinfo is None:
dt = pytz.utc.localize(dt)
dt = dt.replace(tzinfo=timezone.utc)
return int(dt.timestamp() * 1000)


def date_to_perspective(d: date) -> int:
"""Convert a python date to an integer number of milliseconds for perspective >= 3"""
return int(datetime(year=d.year, month=d.month, day=d.day, tzinfo=pytz.UTC).timestamp() * 1000)
return int(datetime(year=d.year, month=d.month, day=d.day, tzinfo=timezone.utc).timestamp() * 1000)
12 changes: 5 additions & 7 deletions csp/impl/wiring/runtime.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import threading
import time
from collections import deque
from datetime import datetime, timedelta

import pytz
from datetime import datetime, timedelta, timezone

from csp.impl.__cspimpl import _cspimpl
from csp.impl.error_handling import ExceptionContext
Expand All @@ -19,15 +17,15 @@
def _normalize_run_times(starttime, endtime, realtime):
if starttime is None:
if realtime:
starttime = datetime.now(pytz.UTC).replace(tzinfo=None)
starttime = datetime.now(timezone.utc).replace(tzinfo=None)
else:
raise RuntimeError("starttime argument is required")
if endtime is None:
raise RuntimeError("endtime argument is required")
if starttime.tzinfo is not None:
starttime = starttime.astimezone(pytz.UTC).replace(tzinfo=None)
starttime = starttime.astimezone(timezone.utc).replace(tzinfo=None)
if isinstance(endtime, datetime) and endtime.tzinfo is not None:
endtime = endtime.astimezone(pytz.UTC).replace(tzinfo=None)
endtime = endtime.astimezone(timezone.utc).replace(tzinfo=None)
if isinstance(endtime, timedelta):
endtime = starttime + endtime
return starttime, endtime
Expand Down Expand Up @@ -202,7 +200,7 @@ def run(
mem_cache.clear(clear_user_objects=False)

# Ensure we dont start running realtime engines before starttime if its in the future
now = datetime.now(pytz.UTC).replace(tzinfo=None)
now = datetime.now(timezone.utc).replace(tzinfo=None)
if starttime > now and realtime:
time.sleep((starttime - now).total_seconds())

Expand Down
22 changes: 11 additions & 11 deletions csp/tests/adapters/test_db.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
import unittest
from datetime import date, datetime, time
from datetime import date, datetime, time, timezone
from zoneinfo import ZoneInfo

import pytz
import sqlalchemy as db

import csp
Expand Down Expand Up @@ -63,7 +63,7 @@ def test_sqlite_basic(self):
engine = self._prepopulate_in_mem_engine()
for time_accessor in (
EngineStartTimeAccessor(),
TimestampAccessor(time_column="TIME", tz=pytz.timezone("US/Eastern")),
TimestampAccessor(time_column="TIME", tz=ZoneInfo("US/Eastern")),
):

def graph():
Expand Down Expand Up @@ -225,7 +225,7 @@ def test_sqlite_constraints(self):
execute_with_commit(engine, query, values_list)

def graph():
time_accessor = DateTimeAccessor(date_column="DATE", time_column="TIME", tz=pytz.timezone("US/Eastern"))
time_accessor = DateTimeAccessor(date_column="DATE", time_column="TIME", tz=ZoneInfo("US/Eastern"))
reader = DBReader.create_from_connection(
connection=engine,
table_name="test",
Expand Down Expand Up @@ -256,7 +256,7 @@ def graph():
csp.add_graph_output("all", all)

# NYC
result = csp.run(graph, starttime=pytz.timezone("US/Eastern").localize(datetime(2020, 3, 3, 9, 30)))
result = csp.run(graph, starttime=datetime(2020, 3, 3, 9, 30, tzinfo=ZoneInfo("US/Eastern")))

self.assertEqual(len(result["aapl"]), 4)
self.assertTrue(all(v[1].SYMBOL == "AAPL" for v in result["aapl"]))
Expand Down Expand Up @@ -338,7 +338,7 @@ def test_join_query(self):
metadata.create_all(engine)

def graph():
time_accessor = TimestampAccessor(time_column="TIME", tz=pytz.timezone("US/Eastern"))
time_accessor = TimestampAccessor(time_column="TIME", tz=ZoneInfo("US/Eastern"))
query = "select * from test1 inner join test2 on test2.TIME=test1.TIME"
reader = DBReader.create_from_connection(
connection=engine, query=query, time_accessor=time_accessor, symbol_column="SYMBOL"
Expand Down Expand Up @@ -366,7 +366,7 @@ def graph():
csp.add_graph_output("all", all)

# NYC
starttime = pytz.timezone("US/Eastern").localize(datetime(2020, 3, 3, 9, 30))
starttime = datetime(2020, 3, 3, 9, 30, tzinfo=ZoneInfo("US/Eastern"))
result = csp.run(graph, starttime=starttime)

# result = csp.run(graph, starttime=datetime(2020, 3, 3, 9, 30))
Expand Down Expand Up @@ -429,7 +429,7 @@ def test_DateTimeAccessor(self):
execute_with_commit(engine, query, values_list)

def graph():
time_accessor = DateTimeAccessor(date_column="DATE", time_column="TIME", tz=pytz.timezone("US/Eastern"))
time_accessor = DateTimeAccessor(date_column="DATE", time_column="TIME", tz=ZoneInfo("US/Eastern"))
reader = DBReader.create_from_connection(
connection=engine, table_name="test", time_accessor=time_accessor, symbol_column="SYMBOL"
)
Expand All @@ -439,7 +439,7 @@ def graph():
csp.add_graph_output("aapl_price", aapl_price)

# NYC
tz = pytz.timezone("US/Eastern")
tz = ZoneInfo("US/Eastern")
periods = [
(datetime(2020, 3, 3), datetime(2020, 3, 4)),
(datetime(2020, 3, 3), datetime(2020, 3, 5)),
Expand All @@ -448,9 +448,9 @@ def graph():
(datetime(2020, 3, 4, 13), datetime(2020, 3, 4, 14)),
]
for starttime, endtime in periods:
result = csp.run(graph, starttime=tz.localize(starttime), endtime=tz.localize(endtime))
result = csp.run(graph, starttime=starttime.replace(tzinfo=tz), endtime=endtime.replace(tzinfo=tz))
target = [
(tz.localize(v[0]).astimezone(pytz.UTC).replace(tzinfo=None), v[1])
(v[0].replace(tzinfo=tz).astimezone(timezone.utc).replace(tzinfo=None), v[1])
for v in values
if v[0] >= starttime and v[0] <= endtime
]
Expand Down
21 changes: 10 additions & 11 deletions csp/tests/adapters/test_parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
import os
import tempfile
import unittest
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone

import numpy
import pandas
import polars
import pyarrow
import pyarrow.parquet
import pytz

import csp
from csp.adapters.output_adapters.parquet import ParquetOutputConfig
Expand Down Expand Up @@ -55,14 +54,14 @@ def write_ts_to_file(x: csp.ts["T"]):
def graph():
if arrow:
reader = ParquetReader(
filename, symbol_column="SYMBOL", time_column="TIME", binary_arrow=arrow, tz=pytz.utc
filename, symbol_column="SYMBOL", time_column="TIME", binary_arrow=arrow, tz=timezone.utc
)
reader_all = ParquetReader(
filename, symbol_column="SYMBOL", time_column="TIME", binary_arrow=arrow, tz=pytz.utc
filename, symbol_column="SYMBOL", time_column="TIME", binary_arrow=arrow, tz=timezone.utc
)
else:
reader = ParquetReader(filename, symbol_column="SYMBOL", time_column="TIME", tz=pytz.utc)
reader_all = ParquetReader(filename, symbol_column="SYMBOL", time_column="TIME", tz=pytz.utc)
reader = ParquetReader(filename, symbol_column="SYMBOL", time_column="TIME", tz=timezone.utc)
reader_all = ParquetReader(filename, symbol_column="SYMBOL", time_column="TIME", tz=timezone.utc)

# Struct
aapl = reader.subscribe("AAPL", PriceQuantity)
Expand Down Expand Up @@ -201,7 +200,7 @@ def graph():
writer.publish("x", x)
writer.publish("y", y)

start_time = datetime(2020, 3, 3, 9, 30, tzinfo=pytz.utc)
start_time = datetime(2020, 3, 3, 9, 30, tzinfo=timezone.utc)
csp.run(graph, starttime=start_time)
df = pandas.read_parquet(filename)
expected_columns = {}
Expand Down Expand Up @@ -235,7 +234,7 @@ def graph():
writer.publish("x", x)
writer.publish("y", y)

start_time = datetime(2020, 3, 3, 9, 30, tzinfo=pytz.utc)
start_time = datetime(2020, 3, 3, 9, 30, tzinfo=timezone.utc)
csp.run(graph, starttime=start_time)

df = pyarrow.RecordBatchStreamReader(pyarrow.memory_map(filename)).read_pandas()
Expand Down Expand Up @@ -791,7 +790,7 @@ def test_string_array_with_nulls(self):
df.to_parquet(temp_file.name)

def reader_g():
reader = ParquetReader(temp_file.name, time_column="t", tz=pytz.utc)
reader = ParquetReader(temp_file.name, time_column="t", tz=timezone.utc)
return reader.subscribe_all(csp.typing.Numpy1DArray[str], "v")

# Null strings in list arrays are read as empty strings (no NaN equivalent for strings)
Expand All @@ -808,7 +807,7 @@ def test_float_array_with_nulls(self):
df.to_parquet(temp_file.name)

def reader_g():
reader = ParquetReader(temp_file.name, time_column="t", tz=pytz.utc)
reader = ParquetReader(temp_file.name, time_column="t", tz=timezone.utc)
return reader.subscribe_all(csp.typing.Numpy1DArray[float], "v")

res = csp.run(reader_g, starttime=datetime(2022, 1, 1), endtime=timedelta(seconds=10))
Expand Down Expand Up @@ -1170,7 +1169,7 @@ def test_tz(file_name: str, start: datetime):
)
writer.publish("int_vals", data)

start = datetime(2020, 1, 1, tzinfo=pytz.utc)
start = datetime(2020, 1, 1, tzinfo=timezone.utc)

with tempfile.NamedTemporaryFile(suffix=".parquet") as output:
output.file.close()
Expand Down
Loading
Loading