Skip to content

Commit

Permalink
Use pandas to_numpy with copy=True
Browse files Browse the repository at this point in the history
  • Loading branch information
larsevj authored and eivindjahren committed Aug 26, 2024
1 parent 4d70d65 commit f3ccfec
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions python/resdata/grid/rd_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -1407,9 +1407,9 @@ def export_data(self, index_frame, kw, default=0):
if not isinstance(index_frame, pandas.DataFrame):
raise TypeError("index_frame must be pandas.DataFrame")
if len(kw) == self.get_global_size():
index = numpy.array(index_frame.index, dtype=numpy.int32)
index = index_frame.index.to_numpy(dtype=numpy.int32, copy=True)
elif len(kw) == self.get_num_active():
index = numpy.array(index_frame["active"], dtype=numpy.int32)
index = index_frame["active"].to_numpy(dtype=numpy.int32, copy=True)
else:
raise ValueError("The keyword must have a 3D compatible length")

Expand Down Expand Up @@ -1444,7 +1444,9 @@ def export_volume(self, index_frame):
Index_fram must be a pandas dataframe with the same structure
as obtained from export_index.
"""
index = numpy.array(index_frame.index, dtype=numpy.int32)
if not isinstance(index_frame, pandas.DataFrame):
raise TypeError("index_frame must be pandas.DataFrame")
index = index_frame.index.to_numpy(dtype=numpy.int32, copy=True)
data = numpy.zeros(len(index), dtype=numpy.float64)
self._export_volume(
len(index),
Expand All @@ -1460,7 +1462,9 @@ def export_position(self, index_frame):
Index_fram must be a pandas dataframe with the same structure
as obtained from export_index.
"""
index = numpy.array(index_frame.index, dtype=numpy.int32)
if not isinstance(index_frame, pandas.DataFrame):
raise TypeError("index_frame must be pandas.DataFrame")
index = index_frame.index.to_numpy(dtype=numpy.int32, copy=True)
data = numpy.zeros([len(index), 3], dtype=numpy.float64)
self._export_position(
len(index),
Expand Down Expand Up @@ -1504,7 +1508,9 @@ def export_corners(self, index_frame):
increase 'towards the sky'; the safest way is probably to check this
explicitly if it matters for the case at hand.
"""
index = numpy.array(index_frame.index, dtype=numpy.int32)
if not isinstance(index_frame, pandas.DataFrame):
raise TypeError("index_frame must be pandas.DataFrame")
index = index_frame.index.to_numpy(dtype=numpy.int32, copy=True)
data = numpy.zeros([len(index), 24], dtype=numpy.float64)
self._export_corners(
len(index),
Expand Down

0 comments on commit f3ccfec

Please sign in to comment.