Skip to content

Commit

Permalink
MNT: Fix up genfromtxt on numpy 2
Browse files Browse the repository at this point in the history
Numpy is returning strings rather than bytes, so we don't always need to
decode.
  • Loading branch information
dopplershift committed Nov 6, 2024
1 parent e5f12e0 commit d763a5a
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/siphon/ncss.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,12 +426,20 @@ def parse_csv_header(line):
return names, units


# Only needed until we support only numpy >= 2.0.
def _decode_if_needed(s):
try:
return s.decode('utf-8')
except AttributeError:
return s


def parse_csv_dataset(data, handle_units):
"""Parse CSV data into a netCDF-like dataset."""
fobj = BytesIO(data)
names, units = parse_csv_header(fobj.readline().decode('utf-8'))
arrs = np.genfromtxt(fobj, dtype=None, names=names, delimiter=',',
converters={'date': lambda s: parse_iso_date(s.decode('utf-8'))})
converters={'date': lambda s: parse_iso_date(_decode_if_needed(s))})
d = {}
for f in arrs.dtype.fields:
dat = arrs[f]
Expand Down

0 comments on commit d763a5a

Please sign in to comment.