Skip to content

Commit

Permalink
Docstrings for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hombit committed Apr 3, 2024
1 parent 1a8125d commit 5f59ea5
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/nested_pandas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .example_module import greetings, meaning
from .series.dtype import NestedDtype

# Import for registering
from .series.accessor import NestSeriesAccessor # noqa: F401
from .series.dtype import NestedDtype

__all__ = ["greetings", "meaning", "NestedDtype"]
2 changes: 1 addition & 1 deletion src/nested_pandas/series/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from numpy.typing import ArrayLike
from pandas.api.extensions import register_series_accessor

from nested_pandas.series.packer import pack_sorted_df_into_struct
from nested_pandas.series.dtype import NestedDtype
from nested_pandas.series.packer import pack_sorted_df_into_struct

__all__ = ["NestSeriesAccessor"]

Expand Down
11 changes: 6 additions & 5 deletions src/nested_pandas/series/dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ def construct_from_string(cls, string: str) -> Self: # type: ignore[name-define
field_strings = fields_str.split(", ")
if len(field_strings) == 0:
raise ValueError(
"Not a valid nested type string, expected at least a single field inside 'nested<x: [type], ...>'"
"Not a valid nested type string, expected at least a single field inside "
"'nested<x: [type], ...>'"
)

fields = {}
Expand All @@ -111,13 +112,13 @@ def construct_from_string(cls, string: str) -> Self: # type: ignore[name-define
field_name, field_type = field_string.split(": ", maxsplit=1)
except ValueError as e:
raise ValueError(
"Not a valid nested type string, expected 'nested<x: [type], ...>', got invalid field string "
f"'{field_string}'"
"Not a valid nested type string, expected 'nested<x: [type], ...>', got invalid field "
f"string '{field_string}'"
) from e
if not field_type.startswith("[") or not field_type.endswith("]"):
raise ValueError(
"Not a valid nested type string, expected 'nested<x: [type], ...>', got invalid field type "
f"string '{field_type}'"
"Not a valid nested type string, expected 'nested<x: [type], ...>', got invalid field "
f"type string '{field_type}'"
)

value_type = field_type.removeprefix("[").removesuffix("]")
Expand Down
29 changes: 25 additions & 4 deletions tests/nested_pandas/series/test_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import pandas as pd
import pyarrow as pa
import pytest
from numpy.testing import assert_array_equal
from pandas.testing import assert_frame_equal, assert_series_equal

from nested_pandas import NestedDtype
from nested_pandas.series.ext_array import NestedExtensionArray
from numpy.testing import assert_array_equal
from pandas.testing import assert_frame_equal, assert_series_equal


def test_registered():
"""Test that the series accessor .nest is registered."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array([np.array([1, 2, 3]), np.array([1.0, 2.0, 1.0])]),
Expand All @@ -23,6 +23,7 @@ def test_registered():


def test_to_lists():
"""Test that the .nest.to_lists() method works."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array([np.array([1.0, 2.0, 3.0]), -np.array([1.0, 2.0, 1.0])]),
Expand Down Expand Up @@ -52,6 +53,7 @@ def test_to_lists():


def test_to_lists_with_fields():
"""Test that the .nest.to_lists(fields=...) method works."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array([np.array([1.0, 2.0, 3.0]), -np.array([1.0, 2.0, 1.0])]),
Expand All @@ -76,6 +78,7 @@ def test_to_lists_with_fields():


def test_to_flat():
"""Test that the .nest.to_flat() method works."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array([np.array([1.0, 2.0, 3.0]), np.array([1.0, 2.0, 1.0])]),
Expand Down Expand Up @@ -112,6 +115,7 @@ def test_to_flat():


def test_to_flat_with_fields():
"""Test that the .nest.to_flat(fields=...) method works."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array([np.array([1.0, 2.0, 3.0]), np.array([1.0, 2.0, 1.0])]),
Expand Down Expand Up @@ -142,6 +146,7 @@ def test_to_flat_with_fields():


def test_fields():
"""Test that the .nest.fields attribute works."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array([np.array([1.0, 2.0, 3.0]), np.array([1.0, 2.0, 1.0])]),
Expand All @@ -155,6 +160,7 @@ def test_fields():


def test_flat_length():
"""Test that the .nest.flat_length attribute works."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array([np.array([1.0, 2.0, 3.0]), np.array([1.0, 2.0, 1.0])]),
Expand All @@ -168,6 +174,7 @@ def test_flat_length():


def test_set_flat_field():
"""Test that the .nest.set_flat_field() method works."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array([np.array([1.0, 2.0, 3.0]), np.array([1.0, 2.0, 1.0])]),
Expand All @@ -191,6 +198,7 @@ def test_set_flat_field():


def test_set_list_field():
"""Test that the .nest.set_list_field() method works."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array([np.array([1.0, 2.0, 3.0]), np.array([1.0, 2.0, 1.0])]),
Expand All @@ -214,6 +222,7 @@ def test_set_list_field():


def test_pop_field():
"""Test that the .nest.pop_field() method works."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array([np.array([1.0, 2.0, 3.0]), np.array([1.0, 2.0, 1.0])]),
Expand All @@ -238,6 +247,7 @@ def test_pop_field():


def test_query_flat_1():
"""Test that the .nest.query_flat() method works."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array([np.array([1.0, 2.0, 3.0]), np.array([4.0, 5.0, 6.0])]),
Expand All @@ -262,7 +272,8 @@ def test_query_flat_1():


# Currently we remove empty rows from the output series
def test_query_flat_2():
def test_query_flat_empty_rows():
"""Test that the .nest.query_flat() method works as expected for empty rows."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array([np.array([1.0, 2.0, 3.0]), np.array([4.0, 5.0, 6.0])]),
Expand All @@ -279,6 +290,7 @@ def test_query_flat_2():


def test_get_list_series():
"""Test that the .nest.get_list_series() method works."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array([np.array([1, 2, 3]), np.array([4, 5, 6])]),
Expand All @@ -302,6 +314,7 @@ def test_get_list_series():


def test___getitem___single_field():
"""Test that the .nest["field"] works for a single field."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array([np.array([1.0, 2.0, 3.0]), np.array([1.0, 2.0, 1.0])]),
Expand Down Expand Up @@ -332,6 +345,7 @@ def test___getitem___single_field():


def test___getitem___multiple_fields():
"""Test that the .nest[["b", "a"]] works for multiple fields."""
arrays = [
pa.array([np.array([1.0, 2.0, 3.0]), -np.array([1.0, 2.0, 1.0])]),
pa.array([np.array([4.0, 5.0, 6.0]), -np.array([3.0, 4.0, 5.0])]),
Expand Down Expand Up @@ -361,6 +375,7 @@ def test___getitem___multiple_fields():


def test___setitem___with_flat():
"""Test that the .nest["field"] = ... works for a single field."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array([np.array([1.0, 2.0, 3.0]), np.array([1.0, 2.0, 1.0])]),
Expand All @@ -384,6 +399,7 @@ def test___setitem___with_flat():


def test___setitem___with_list():
"""Test that the .nest["field"] = ... works for a single field."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array([np.array([1.0, 2.0, 3.0]), np.array([1.0, 2.0, 1.0])]),
Expand All @@ -407,6 +423,7 @@ def test___setitem___with_list():


def test___setited___raises_for_ambiguous_lengths_1():
"""Test that the .nest["field"] = ... raises for ambiguous lengths of the right hand side."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array(
Expand All @@ -430,6 +447,7 @@ def test___setited___raises_for_ambiguous_lengths_1():


def test___setited___raises_for_ambiguous_lengths_2():
"""Test that the .nest["field"] = ... raises for ambiguous lengths of the right hand side."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array([np.array([1.0, 2.0]), np.array([])]),
Expand All @@ -444,6 +462,7 @@ def test___setited___raises_for_ambiguous_lengths_2():


def test___delitem__():
"""Test that the `del .nest["field"]` works."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array([np.array([1.0, 2.0, 3.0]), np.array([1.0, 2.0, 1.0])]),
Expand All @@ -459,6 +478,7 @@ def test___delitem__():


def test___iter__():
"""Test that the iter(.nest) works."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array([np.array([1.0, 2.0, 3.0]), np.array([1.0, 2.0, 1.0])]),
Expand All @@ -472,6 +492,7 @@ def test___iter__():


def test___len__():
"""Test that the len(.nest) works."""
struct_array = pa.StructArray.from_arrays(
arrays=[
pa.array([np.array([1.0, 2.0, 3.0]), np.array([1.0, 2.0, 1.0])]),
Expand Down
6 changes: 5 additions & 1 deletion tests/nested_pandas/series/test_dtype.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pyarrow as pa
import pytest

from nested_pandas.series.dtype import NestedDtype
from nested_pandas.series.ext_array import NestedExtensionArray

Expand All @@ -19,6 +18,7 @@
],
)
def test_from_pyarrow_dtype(pyarrow_dtype):
"""Test that we can construct NestedDtype from pyarrow struct type."""
dtype = NestedDtype(pyarrow_dtype)
assert dtype.pyarrow_dtype == pyarrow_dtype

Expand All @@ -35,11 +35,13 @@ def test_from_pyarrow_dtype(pyarrow_dtype):
],
)
def test_from_pyarrow_dtype_raises(pyarrow_dtype):
"""Test that we raise an error when constructing NestedDtype from invalid pyarrow type."""
with pytest.raises(ValueError):
NestedDtype(pyarrow_dtype)


def test_from_fields():
"""Test NestedDtype.from_fields()."""
fields = {"a": pa.int64(), "b": pa.float64()}
dtype = NestedDtype.from_fields(fields)
assert dtype.pyarrow_dtype == pa.struct(
Expand All @@ -60,9 +62,11 @@ def test_from_fields():
],
)
def test_name_vs_construct_from_string(fields):
"""Test that dtype.name is consistent with dtype.construct_from_string(dtype.name)."""
dtype = NestedDtype.from_fields(fields)
assert dtype == NestedDtype.construct_from_string(dtype.name)


def test_construct_array_type():
"""Test that NestedDtype.construct_array_type() returns NestedExtensionArray."""
assert NestedDtype.construct_array_type() is NestedExtensionArray
Loading

0 comments on commit 5f59ea5

Please sign in to comment.