Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
Replace custom assert fns with pandas'
Browse files Browse the repository at this point in the history
  • Loading branch information
hombit committed Feb 2, 2024
1 parent 06a3602 commit 80c6d13
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 42 deletions.
15 changes: 0 additions & 15 deletions tests/pandas_ts/conftest.py
Original file line number Diff line number Diff line change
@@ -1,15 +0,0 @@
from numpy.testing import assert_array_equal


def assert_nested_array_series_equal(a, b):
assert_array_equal(a.index, b.index)
for inner_a, inner_b in zip(a, b):
assert_array_equal(inner_a, inner_b, err_msg=f"Series '{a.name}' is not equal series '{b.name}'")


def assert_df_equal(a, b):
assert_array_equal(a.index, b.index)
assert_array_equal(a.columns, b.columns)
assert_array_equal(a.dtypes, b.dtypes)
for column in a.columns:
assert_array_equal(a[column], b[column], err_msg=f"Column '{column}' is not equal column '{column}'")
23 changes: 7 additions & 16 deletions tests/pandas_ts/test_packer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import numpy as np
import pandas as pd
import pyarrow as pa
from conftest import assert_nested_array_series_equal
from numpy.testing import assert_array_equal
from pandas.testing import assert_frame_equal, assert_series_equal

from pandas_ts import packer

Expand Down Expand Up @@ -54,13 +54,7 @@ def test_pack_df():
},
)

assert_array_equal(actual.index, desired.index)
assert_array_equal(actual.dtypes, desired.dtypes)
assert_array_equal(actual.columns, desired.columns)
assert actual.shape == desired.shape

for column in actual.columns:
assert_nested_array_series_equal(actual[column], desired[column])
assert_frame_equal(actual, desired)


def test_pack_df_into_structs():
Expand All @@ -86,8 +80,7 @@ def test_pack_df_into_structs():
),
)

assert actual.dtype == desired.dtype
assert_nested_array_series_equal(actual, desired)
assert_series_equal(actual, desired)


def test_pack_sorted_df_into_struct():
Expand All @@ -113,8 +106,7 @@ def test_pack_sorted_df_into_struct():
),
)

assert actual.dtype == desired.dtype
assert_nested_array_series_equal(actual, desired)
assert_series_equal(actual, desired)


def test_view_packed_df_as_struct_series():
Expand All @@ -139,7 +131,7 @@ def test_view_packed_df_as_struct_series():
series = packer.view_packed_df_as_struct_series(packed_df)

for field_name in packed_df.columns:
assert_nested_array_series_equal(series.struct.field(field_name), packed_df[field_name])
assert_series_equal(series.struct.field(field_name), packed_df[field_name])


def test_view_sorted_df_as_nested_arrays():
Expand Down Expand Up @@ -172,8 +164,7 @@ def test_view_sorted_df_as_nested_arrays():
index=[1, 2, 3, 4],
dtype=pd.ArrowDtype(pa.list_(pa.int64())),
)
for column in nested_df:
assert_nested_array_series_equal(nested_df[column], desired_nested[column])
assert_frame_equal(nested_df, desired_nested)

assert_array_equal(nested_df.attrs["_offset"], [0, 2, 4, 6, 9])
assert_array_equal(nested_df.attrs["_offset"], nested_df["a"].attrs["_offset"])
Expand All @@ -199,7 +190,7 @@ def test_view_sorted_series_as_nested_array():
index=[1, 2, 3, 4],
dtype=pd.ArrowDtype(pa.list_(pa.int64())),
)
assert_nested_array_series_equal(nested, desired_nested)
assert_series_equal(nested, desired_nested)

assert_array_equal(nested.attrs["_offset"], [0, 2, 4, 6, 9])

Expand Down
19 changes: 8 additions & 11 deletions tests/pandas_ts/test_ts_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import pandas as pd
import pyarrow as pa
import pytest
from conftest import assert_df_equal, assert_nested_array_series_equal
from numpy.testing import assert_array_equal
from pandas.testing import assert_frame_equal, assert_series_equal


@pytest.mark.skip("It is really tricky to test it: some other test may import pandas-ts before this one")
Expand Down Expand Up @@ -95,12 +95,7 @@ def test_ts_accessor_to_nested():
),
},
)

assert_array_equal(nested.dtypes, desired.dtypes)
assert_array_equal(nested.index, desired.index)

for column in nested.columns:
assert_nested_array_series_equal(nested[column], desired[column])
assert_frame_equal(nested, desired)


def test_ts_accessor_to_flat():
Expand Down Expand Up @@ -168,20 +163,22 @@ def test_ts_accessor___getitem__():
)
series = pd.Series(struct_array, dtype=pd.ArrowDtype(struct_array.type), index=[0, 1])

assert_nested_array_series_equal(
assert_series_equal(
series.ts["a"],
pd.Series(
[np.array([1.0, 2.0, 3.0]), np.array([1.0, 2.0, 1.0])],
dtype=pd.ArrowDtype(pa.list_(pa.float64())),
index=[0, 1],
name="a",
),
)
assert_nested_array_series_equal(
assert_series_equal(
series.ts["b"],
pd.Series(
[-np.array([4.0, 5.0, 6.0]), -np.array([3.0, 4.0, 5.0])],
dtype=pd.ArrowDtype(pa.list_(pa.float64())),
index=[0, 1],
name="b",
),
)

Expand All @@ -199,7 +196,7 @@ def test_ts_accessor_get():
series = pd.Series(struct_array, dtype=pd.ArrowDtype(struct_array.type), index=[100, 101])

second_row_as_df = series.ts.get(101)
assert_df_equal(
assert_frame_equal(
second_row_as_df, pd.DataFrame({"a": np.array([1.0, 2.0, 1.0]), "b": -np.array([3.0, 4.0, 5.0])})
)

Expand All @@ -218,6 +215,6 @@ def test_ts_accessor_iget():
series = pd.Series(struct_array, dtype=pd.ArrowDtype(struct_array.type), index=[100, 101])

first_row_as_df = series.ts.iget(-2)
assert_df_equal(
assert_frame_equal(
first_row_as_df, pd.DataFrame({"a": np.array([1.0, 2.0, 3.0]), "b": -np.array([4.0, 5.0, 6.0])})
)

0 comments on commit 80c6d13

Please sign in to comment.