Skip to content

Commit

Permalink
upscale int8 to int16 for numpy 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ocefpaf committed Jun 18, 2024
1 parent 79c4228 commit 0906ca6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pocean/dsg/trajectoryProfile/cr.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
get_ncdata_from_series,
nativize_times,
normalize_countable_array,
upscale_int8,
)


Expand Down Expand Up @@ -323,6 +324,7 @@ def to_dataframe(self, clean_cols=True, clean_rows=True, **kwargs):
df_data[dnam] = vdata

df = pd.DataFrame(df_data)
df = upscale_int8(df)

# Drop all data columns with no data
if clean_cols:
Expand Down
2 changes: 1 addition & 1 deletion pocean/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def test_generic_masked_bad_min_max_value(self):
# to make sure it doesn't error
b = ncd.createVariable('imabyte', 'b')
b.valid_min = 0
b.valid_max = 600 # this is over a byte and thus invalid
b.valid_max = np.int16(600) # this is over a byte and thus invalid
b[:] = 3
r = generic_masked(b[:], attrs=ncd.vatts(b.name))
assert np.all(r.mask == False) # noqa
Expand Down
7 changes: 7 additions & 0 deletions pocean/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,13 @@ def dict_update(d, u):
return d


def upscale_int8(df):
"""Numpy 2.0 no linger upcast dtypes.
In order to preserve the data's original dtype we upcast it here after reading it.
"""
return df.astype({col: "int16" for col in df.columns[df.dtypes == "int8"]})

class JSONEncoder(json.JSONEncoder):

def default(self, obj):
Expand Down

0 comments on commit 0906ca6

Please sign in to comment.