Skip to content

Commit 54c829a

Browse files
committed
Refactor assert_cftime_like_particlefile
1 parent e7e37ef commit 54c829a

1 file changed

Lines changed: 6 additions & 14 deletions

File tree

tests/utils.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,10 @@ def round_and_hash_float_array(arr, decimals=6):
160160
def assert_cftime_like_particlefile(parquet_path: Path) -> None:
161161
assert parquet_path.suffix == ".parquet", "Path must be a parquet file"
162162

163-
table = pq.read_table(parquet_path)
164-
assert "time" in table.schema.names, "Parquet file must have a 'time' column"
165-
166-
time_field = table.schema.field("time")
167-
assert pa.types.is_floating(time_field.type) or pa.types.is_integer(time_field.type), (
168-
f"'time' column must be numeric, got {time_field.type}"
169-
)
170-
171-
attrs = {k.decode(): v.decode() for k, v in time_field.metadata.items()}
172-
173-
values = table.column("time").to_pylist()
174-
v = xr.Variable(("time",), values, attrs)
175-
decoded = xr.coders.CFDatetimeCoder(time_unit="s").decode(v)
163+
df = read_particlefile(parquet_path, decode_times=True)
176164

177165
# check first value (and hence rest of array) is what we expect
178-
assert isinstance(decoded.values[0], (cftime.datetime, np.datetime64)), (
166+
assert isinstance(df["time"].values[0], (cftime.datetime, np.datetime64)), (
179167
"CF-time values in Parquet did not get properly decoded. Are the attributes correct?"
180168
)
181169
return
@@ -193,6 +181,10 @@ def read_particlefile(path: Path, decode_times: bool = True) -> pd.DataFrame:
193181
f"Could not find 'time' column in parquet file. Are you sure {path=!r} is a particlefile?"
194182
) from e
195183

184+
assert pa.types.is_floating(time_field.type) or pa.types.is_integer(time_field.type), (
185+
f"'time' column must be numeric, got {time_field.type}"
186+
)
187+
196188
try:
197189
assert b"units" in time_field.metadata
198190
except AssertionError as e:

0 commit comments

Comments
 (0)