Skip to content
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

Release timezone and calendar features in DatapointsAPI (beta) #1779

Merged
merged 47 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
9f3ed12
add warning to ret..in_tz
haakonvt May 16, 2024
fa358c4
cleanup of zoneinfo: type annots. and importing
haakonvt May 23, 2024
55fc0c8
change datetime imports to avoid timezone ambiguity
haakonvt May 23, 2024
a942ea9
force serial fetching for tz + calendar queries
haakonvt May 23, 2024
c5a7e33
add cursor support for tz+calender queries
haakonvt May 23, 2024
b2f613d
fix docstring test that failed
haakonvt May 23, 2024
bf05a64
several minor bugfixes. in chunking mode, use first cursor
haakonvt May 23, 2024
251c33f
add timezone for Datapoint
haakonvt May 24, 2024
9da1c3a
add tests for timezone for Datapoint
haakonvt May 24, 2024
a95c19f
do not allow str timezone (we cant display it)
haakonvt May 24, 2024
df0fa79
make lint CI build job have all deps for mypy inferrence
haakonvt May 24, 2024
b36b3fc
add timezone support in display method for all dps classes
haakonvt May 25, 2024
21260fe
fix logic on uniform_index and tz/cal
haakonvt May 26, 2024
cf7680c
dump timezone once for Datapoints.dump
haakonvt May 26, 2024
32ecd5a
fix fakecogresgen for dps and timezone
haakonvt May 26, 2024
235692c
support timezone for raw dps queries
haakonvt May 26, 2024
1583e7b
update Datapoint tests
haakonvt May 26, 2024
fefdf80
fix more automated tests
haakonvt May 26, 2024
da676ab
actually support timezone for raw dps queries
haakonvt May 26, 2024
b6c8948
third time's the charm
haakonvt May 26, 2024
d41094c
some docs updates
haakonvt May 26, 2024
81953fd
fix timezone in dps classes: dump/iter/str/to_pandas
haakonvt May 27, 2024
f339c26
never mutate user dps query
haakonvt May 27, 2024
3f3eb13
fix timezone not being passed on in retrieve_dataframe
haakonvt May 27, 2024
82d3b56
add integ.test test_timezone_raw_query_dst_transitions
haakonvt May 27, 2024
3eccb20
add integ. test test_timezone_agg_query_dst_transitions
haakonvt May 27, 2024
0d03514
add test for calendar granularities
haakonvt May 28, 2024
41b6466
improve error message on _verify_time_range to include user given (no…
haakonvt May 28, 2024
00b333e
handle beta header for user with warning
haakonvt May 28, 2024
996ce6d
changelog, version
haakonvt May 28, 2024
07ad634
make backports-zoneinfo required on 3.8
haakonvt May 29, 2024
04bf8e4
update zoneinfo imports after required change
haakonvt May 29, 2024
ecbc535
support string timezone
haakonvt May 29, 2024
3eb4d61
reorder tests to their appropriate classes
haakonvt May 29, 2024
89d7d5b
documentation changes after adding str + tz/month example code
haakonvt May 29, 2024
b6de2d7
fix DatapointsArray.load for timezone
haakonvt May 29, 2024
58f65a9
add test for parse_str_timezone
haakonvt May 30, 2024
7f37fd3
move retrieve_dataframe_in_tz "to use API"
haakonvt May 30, 2024
67baf98
update test using DayAligner
haakonvt May 31, 2024
d4f6184
doc improvements re pr feedback
haakonvt May 31, 2024
a292704
change dt.foo to datetime.foo
haakonvt May 31, 2024
0aef557
Revert "update test using DayAligner"
haakonvt May 31, 2024
e5f04f6
Revert "move retrieve_dataframe_in_tz "to use API""
haakonvt May 31, 2024
b5389c4
more dps doc improvements
haakonvt May 31, 2024
41fa87a
dont warn when still beta
haakonvt May 31, 2024
44f54e8
deactivate groups create tests due to max API limit
haakonvt May 31, 2024
756a4f6
last round of fixes (DatapointsList.__str__)
haakonvt May 31, 2024
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 .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup
with:
extras: '-E numpy'
extras: '-E pandas'
- name: Linting and static code checks
run: pre-commit run --all-files

Expand Down
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@ Changes are grouped as follows
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [7.45.0] - 2024-05-28
### Added
- DatapointsAPI now support `timezone` and new calendar-based granularities like `month`, `quarter` and `year`.
These API features are in beta, and the SDK implementation in alpha, meaning breaking changes can
occur without warning. Set beta header to avoid warning. Users of `retrieve_dataframe_in_tz` should
consider preparing to upgrade as soon as the features reach general availability (GA).

## [7.44.1] - 2024-05-24
### Added
- Missing parameter `timeout` to `client.transformations.preview`.
- Missing parameter `timeout` to `client.transformations.preview`.

## [7.44.0] - 2024-05-24
### Added
- New utility function `datetime_to_ms_iso_timestamp` in `cognite.client.utils` to convert a datetime object to a string representing a timestamp in the format expected by the Cognite GraphQL API.
- New utility function `datetime_to_ms_iso_timestamp` in `cognite.client.utils` to convert a datetime object
to a string representing a timestamp in the format expected by the Cognite GraphQL API.

## [7.43.6] - 2024-05-27
### Improved
Expand Down
184 changes: 123 additions & 61 deletions cognite/client/_api/datapoint_tasks.py

Large diffs are not rendered by default.

213 changes: 126 additions & 87 deletions cognite/client/_api/datapoints.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cognite/client/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import annotations

__version__ = "7.44.1"
__version__ = "7.45.0"
__api_subversion__ = "20230101"
228 changes: 151 additions & 77 deletions cognite/client/data_classes/datapoints.py

Large diffs are not rendered by default.

11 changes: 1 addition & 10 deletions cognite/client/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
from __future__ import annotations

import contextlib
import sys

from cognite.client.utils._time import (
MAX_TIMESTAMP_MS,
MIN_TIMESTAMP_MS,
ZoneInfo,
datetime_to_ms,
datetime_to_ms_iso_timestamp,
ms_to_datetime,
timestamp_to_ms,
)

# Needed for doctest to pass.
with contextlib.suppress(ImportError):
if sys.version_info >= (3, 9):
from zoneinfo import ZoneInfo
else:
from backports.zoneinfo import ZoneInfo

__all__ = [
"ZoneInfo",
"MAX_TIMESTAMP_MS",
Expand Down
1 change: 1 addition & 0 deletions cognite/client/utils/_importing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
if TYPE_CHECKING:
from concurrent.futures import Future


_T = TypeVar("_T")


Expand Down
48 changes: 46 additions & 2 deletions cognite/client/utils/_pandas_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

import re
import warnings
from datetime import timezone
from inspect import signature
from itertools import chain
from numbers import Integral
from typing import TYPE_CHECKING, Any, Sequence
from typing import TYPE_CHECKING, Any, Literal, Sequence

from cognite.client.exceptions import CogniteImportError
from cognite.client.utils._importing import local_import
from cognite.client.utils._text import to_camel_case
from cognite.client.utils._time import TIME_ATTRIBUTES
from cognite.client.utils._time import TIME_ATTRIBUTES, ZoneInfo

if TYPE_CHECKING:
import pandas as pd

from cognite.client.data_classes import DatapointsArrayList, DatapointsList
from cognite.client.data_classes._base import T_CogniteResource, T_CogniteResourceList


Expand Down Expand Up @@ -43,6 +45,48 @@ def pandas_major_version() -> int:
return int(__version__.split(".")[0])


def convert_tz_for_pandas(tz: str | timezone | ZoneInfo | None) -> str | timezone | None:
if tz is None or isinstance(tz, (str, timezone)):
return tz
if isinstance(tz, ZoneInfo):
# pandas is not happy about ZoneInfo :shrug:
if tz.key is not None:
return tz.key
raise ValueError("timezone of type ZoneInfo does not have the required 'key' attribute set")
raise ValueError(f"'timezone' not understood, expected one of: [None, datetime.timezone, ZoneInfo], got {type(tz)}")


def concat_dps_dataframe_list(
dps_lst: DatapointsList | DatapointsArrayList,
column_names: Literal["id", "external_id"],
include_aggregate_name: bool,
include_granularity_name: bool,
include_status: bool,
) -> pd.DataFrame:
pd = local_import("pandas")
dfs = [
dps.to_pandas(
column_names=column_names,
include_aggregate_name=include_aggregate_name,
include_granularity_name=include_granularity_name,
include_status=include_status,
)
for dps in dps_lst
]
if not dfs:
return pd.DataFrame(index=pd.to_datetime([]))
timezones = set(dps.timezone for dps in dps_lst) - {None}
# If attempting to join naive & aware, pandas will raise (so we don't need to):
# TypeError: Cannot join tz-naive with tz-aware DatetimeIndex
if len(timezones) > 1:
warnings.warn(
f"When concatenating datapoints localized to different timezones ({sorted(map(str, timezones))}), the "
"final dataframe index (timestamps) will be a union of the UTC converted timestamps.",
UserWarning,
)
return concat_dataframes_with_nullable_int_cols(dfs)


def notebook_display_with_fallback(inst: T_CogniteResource | T_CogniteResourceList, **kwargs: Any) -> str:
if "camel_case" in signature(inst.to_pandas).parameters:
# Default of False enforced (when accepted by method):
Expand Down
Loading