From 990d0666cc1e6af03717134784ee2f2b5e50d5fd Mon Sep 17 00:00:00 2001 From: Henrik Andersson Date: Sun, 17 Mar 2024 10:31:49 +0100 Subject: [PATCH] Remove duplicate iteminfo construction --- mikeio/dfs/_dfs0.py | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/mikeio/dfs/_dfs0.py b/mikeio/dfs/_dfs0.py index 7e28fe094..8dd5603c6 100644 --- a/mikeio/dfs/_dfs0.py +++ b/mikeio/dfs/_dfs0.py @@ -157,7 +157,7 @@ def read( raise FileNotFoundError(f"File {path} not found") # read data from file - fdata, ftime, fitems = self._read(self._filename) + fdata, ftime = self._read(self._filename) dfs = self._dfs # select items @@ -165,7 +165,9 @@ def read( item_numbers = _valid_item_numbers(dfs.ItemInfo, items) if items is not None: fdata = [fdata[it] for it in item_numbers] - fitems = [fitems[it] for it in item_numbers] + fitems = [self.items[it] for it in item_numbers] + else: + fitems = self.items ds = Dataset(fdata, ftime, fitems, validate=False) # select time steps @@ -199,9 +201,7 @@ def read( return ds - def _read( - self, filename: str - ) -> tuple[list[np.ndarray], pd.DatetimeIndex, list[ItemInfo]]: + def _read(self, filename: str) -> tuple[list[np.ndarray], pd.DatetimeIndex]: """ Read all data from a dfs0 file. """ @@ -222,17 +222,7 @@ def _read( time = pd.to_datetime(t_seconds, unit="s", origin=self.start_time) time = time.round(freq="ms") # accept nothing finer than milliseconds - items = [ - ItemInfo( - item.Name, - EUMType(item.Quantity.Item), - EUMUnit(item.Quantity.Unit), - data_value_type=item.ValueType, - ) - for item in self._dfs.ItemInfo - ] - - return data, time, items + return data, time @staticmethod def _to_dfs_datatype(dtype: Any = None) -> DfsSimpleType: @@ -263,7 +253,8 @@ def to_dataframe( ------- pd.DataFrame """ - data, time, items = self._read(self._filename) + data, time = self._read(self._filename) + items = self.items if unit_in_name: cols = [f"{item.name} ({item.unit.name})" for item in items] else: