Skip to content

Commit 489ef7a

Browse files
committed
update pandas tests to work with v3
1 parent f9a5344 commit 489ef7a

8 files changed

Lines changed: 58 additions & 47 deletions

File tree

tests/tests_integration/test_api/test_datapoint_subscriptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def test_iterate_data_subscription_datapoints_added(
323323
{time_series_external_ids[0]: new_values},
324324
index=pd.date_range(start=start, periods=2, freq="1d"),
325325
)
326-
new_timestamps = new_data.index.asi8 // 10**6
326+
new_timestamps = new_data.index.as_unit("ms").asi8
327327
try:
328328
cognite_client.time_series.data.insert_dataframe(new_data)
329329
batch = next(cognite_client.time_series.subscriptions.iterate_data(new_subscription.external_id))

tests/tests_integration/test_api/test_datapoints.py

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,24 +1577,28 @@ def test_query_no_ts_exists(self, retrieve_endpoints: list[Callable]) -> None:
15771577
def test_timezone_raw_query_dst_transitions(
15781578
self, all_retrieve_endpoints: list[Callable], dps_queries_dst_transitions: list[DatapointsQuery]
15791579
) -> None:
1580-
expected_index = pd.to_datetime(
1581-
[
1582-
# to summer
1583-
"1991-03-31 00:20:05.911+01:00",
1584-
"1991-03-31 00:39:49.780+01:00",
1585-
"1991-03-31 03:21:08.144+02:00",
1586-
"1991-03-31 03:28:06.963+02:00",
1587-
"1991-03-31 03:28:51.903+02:00",
1588-
# to winter
1589-
"1991-09-29 01:02:37.949+02:00",
1590-
"1991-09-29 02:09:29.699+02:00",
1591-
"1991-09-29 02:11:39.983+02:00",
1592-
"1991-09-29 02:10:59.442+01:00",
1593-
"1991-09-29 02:52:26.212+01:00",
1594-
"1991-09-29 04:12:02.558+01:00",
1595-
],
1596-
utc=True, # pandas is not great at parameter names
1597-
).tz_convert("Europe/Oslo")
1580+
expected_index = (
1581+
pd.to_datetime(
1582+
[
1583+
# to summer
1584+
"1991-03-31 00:20:05.911+01:00",
1585+
"1991-03-31 00:39:49.780+01:00",
1586+
"1991-03-31 03:21:08.144+02:00",
1587+
"1991-03-31 03:28:06.963+02:00",
1588+
"1991-03-31 03:28:51.903+02:00",
1589+
# to winter
1590+
"1991-09-29 01:02:37.949+02:00",
1591+
"1991-09-29 02:09:29.699+02:00",
1592+
"1991-09-29 02:11:39.983+02:00",
1593+
"1991-09-29 02:10:59.442+01:00",
1594+
"1991-09-29 02:52:26.212+01:00",
1595+
"1991-09-29 04:12:02.558+01:00",
1596+
],
1597+
utc=True, # pandas is not great at parameter names
1598+
)
1599+
.tz_convert("Europe/Oslo")
1600+
.as_unit("ms")
1601+
)
15981602
expected_to_summer_index = expected_index[:5]
15991603
expected_to_winter_index = expected_index[5:]
16001604
for endpoint, convert in zip(all_retrieve_endpoints, (True, True, False)):
@@ -2201,21 +2205,25 @@ def test_timezone_agg_query_dst_transitions(
22012205
) -> None:
22022206
expected_values1 = [-0.0408386913634, -0.1204416510548, -0.1519269888052, 0.00331827604225]
22032207
expected_values2 = [-0.0503489023269, 0.190474485259, 0.102249925079, -0.1000846729966]
2204-
expected_index = pd.to_datetime(
2205-
[
2206-
# to summer
2207-
"2023-03-26 00:00:00+01:00",
2208-
"2023-03-26 01:00:00+01:00",
2209-
"2023-03-26 03:00:00+02:00",
2210-
"2023-03-26 04:00:00+02:00",
2211-
# to winter
2212-
"2023-10-29 01:00:00+02:00",
2213-
"2023-10-29 02:00:00+02:00",
2214-
"2023-10-29 02:00:00+01:00",
2215-
"2023-10-29 03:00:00+01:00",
2216-
],
2217-
utc=True, # pandas is still not great at parameter names
2218-
).tz_convert("Europe/Oslo")
2208+
expected_index = (
2209+
pd.to_datetime(
2210+
[
2211+
# to summer
2212+
"2023-03-26 00:00:00+01:00",
2213+
"2023-03-26 01:00:00+01:00",
2214+
"2023-03-26 03:00:00+02:00",
2215+
"2023-03-26 04:00:00+02:00",
2216+
# to winter
2217+
"2023-10-29 01:00:00+02:00",
2218+
"2023-10-29 02:00:00+02:00",
2219+
"2023-10-29 02:00:00+01:00",
2220+
"2023-10-29 03:00:00+01:00",
2221+
],
2222+
utc=True, # pandas is still not great at parameter names
2223+
)
2224+
.tz_convert("Europe/Oslo")
2225+
.as_unit("ms")
2226+
)
22192227
expected_to_summer_index = expected_index[:4]
22202228
expected_to_winter_index = expected_index[4:]
22212229
for endpoint, convert in zip(all_retrieve_endpoints, (True, True, False)):
@@ -2449,7 +2457,8 @@ def test_raw_dps(
24492457
assert res_df.isna().sum().sum() == 0
24502458
assert res_df.shape == (exp_len, n_ts)
24512459
assert res_df.dtypes.nunique() == 1
2452-
assert res_df.dtypes.iloc[0] == exp_dtype
2460+
dtype = res_df.dtypes.iloc[0]
2461+
assert dtype == exp_dtype or (exp_dtype is object and pd.api.types.is_string_dtype(dtype))
24532462

24542463
@pytest.mark.parametrize("uniform, exp_n_ts_delta, exp_n_nans_step_interp", ((True, 1, 1), (False, 2, 0)))
24552464
def test_agg_uniform_true_false(
@@ -2504,8 +2513,8 @@ def test_low_limits(
25042513
)
25052514
# We have duplicates in df.columns, so to test specific columns, we reset first:
25062515
res_df.columns = c1, c2, c3, c4, *cx = range(len(res_df.columns))
2507-
assert res_df[[c1, c2]].dtypes.unique() == [object]
2508-
assert res_df[[c3, c4, *cx]].dtypes.unique() == [np.float64]
2516+
assert all(pd.api.types.is_string_dtype(dt) for dt in res_df[[c1, c2]].dtypes)
2517+
assert (res_df[[c3, c4, *cx]].dtypes == np.float64).all()
25092518
assert (res_df[[c1, c3, *cx]].count() == [limit] * (len(cx) + 2)).all()
25102519
assert (res_df[[c2, c4]].count() == [limit + 2] * 2).all()
25112520

tests/tests_unit/test_api/test_raw.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,8 @@ def test_dbs_to_pandas(self) -> None:
652652
pd.DataFrame({"name": ["kar", "car", "dar"]}), db_list.to_pandas().drop("created_time", axis=1)
653653
)
654654
pd.testing.assert_frame_equal(
655-
pd.DataFrame({"value": ["kar"]}, index=["name"]), db_list[0].to_pandas().drop("created_time")
655+
pd.DataFrame({"value": ["kar"]}, index=["name"], dtype=object),
656+
db_list[0].to_pandas().drop("created_time"),
656657
)
657658

658659
def test_tables_to_pandas(self) -> None:
@@ -666,7 +667,8 @@ def test_tables_to_pandas(self) -> None:
666667
pd.DataFrame({"name": ["kar", "car", "dar"]}), table_list.to_pandas().drop("created_time", axis=1)
667668
)
668669
pd.testing.assert_frame_equal(
669-
pd.DataFrame({"value": ["kar"]}, index=["name"]), table_list[0].to_pandas().drop("created_time")
670+
pd.DataFrame({"value": ["kar"]}, index=["name"], dtype=object),
671+
table_list[0].to_pandas().drop("created_time"),
670672
)
671673

672674
def test_rows_to_pandas(self) -> None:

tests/tests_unit/test_api/test_sequences.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,8 +740,8 @@ def test_sequences_to_pandas(self, cognite_client: CogniteClient, mock_seq_respo
740740
df = seq.to_pandas(expand_metadata=True, metadata_prefix="")
741741
assert isinstance(df, pd.DataFrame)
742742
assert "metadata" not in df.columns
743-
assert "string" == df.loc["description"][0]
744-
assert "metadata-value" == df.loc["metadata-key"][0]
743+
assert "string" == df.loc["description"].iloc[0]
744+
assert "metadata-value" == df.loc["metadata-key"].iloc[0]
745745

746746
def test_insert_dataframe_extids(self, cognite_client: CogniteClient, mock_post_sequence_data: HTTPXMock) -> None:
747747
import pandas as pd

tests/tests_unit/test_api/test_time_series.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,5 +259,5 @@ def test_time_series_to_pandas(self, cognite_client: CogniteClient, mock_ts_resp
259259
df = ts.to_pandas(expand_metadata=True, metadata_prefix="", camel_case=True)
260260
assert isinstance(df, pd.DataFrame)
261261
assert "metadata" not in df.columns
262-
assert [0] == df.loc["securityCategories"][0]
263-
assert "metadata-value" == df.loc["metadata-key"][0]
262+
assert [0] == df.loc["securityCategories"].iloc[0]
263+
assert "metadata-value" == df.loc["metadata-key"].iloc[0]

tests/tests_unit/test_data_classes/test_data_models/test_typed_instances.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ def test_to_pandas_list(self, person_read: PersonRead) -> None:
305305
"space": ["sp_my_fixed_space"],
306306
"external_id": ["my_external_id"],
307307
"version": [1],
308-
"last_updated_time": [pd.Timestamp("1970-01-01 00:00:00")],
309-
"created_time": [pd.Timestamp("1970-01-01 00:00:00")],
308+
"last_updated_time": [pd.Timestamp(0, unit="ms")],
309+
"created_time": [pd.Timestamp(0, unit="ms")],
310310
"instance_type": ["node"],
311311
"type": [{"space": "sp_model_space", "external_id": "person"}],
312312
"name": ["John Doe"],

tests/tests_unit/test_data_classes/test_datapoints.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def test_identifier_priority(self, dps_lst_cls: type[CogniteResourceList]) -> No
111111

112112
exp_df = pd.DataFrame(
113113
{1: 2.0, 2: 4.0, 3: 6.0},
114-
index=np.array([1234 * 1_000_000], dtype="datetime64[ns]"),
114+
index=np.array([1234], dtype="datetime64[ms]"),
115115
)
116116
exp_df.columns = pd.MultiIndex.from_tuples(
117117
[(123,), ("foo",), (NodeId(space="s", external_id="x"),)], names=["identifier"]

tests/tests_unit/test_data_classes/test_groups.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class TestGroupsList:
104104
@pytest.mark.parametrize(
105105
"convert_timestamps, expected",
106106
(
107-
(True, dict(data=[None, "1970-01-02 10:17:36.789", None], dtype="datetime64[ns]", name="deleted_time")),
107+
(True, dict(data=[None, "1970-01-02 10:17:36.789", None], dtype="datetime64[ms]", name="deleted_time")),
108108
(False, dict(data=[-1, 123456789, None], dtype="Int64", name="deleted_time")),
109109
),
110110
)

0 commit comments

Comments
 (0)