ts_reader.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import pandas as pd
  15. from ...utils.benchmark import benchmark
  16. from ...utils.io import CSVReader
  17. @benchmark.timeit_with_options(name=None, is_read_operation=True)
  18. class ReadTS:
  19. def __init__(self):
  20. super().__init__()
  21. self._reader = CSVReader(backend="pandas")
  22. def __call__(self, ts_list):
  23. """apply"""
  24. return [self.read(ts) for ts in ts_list]
  25. def read(self, ts):
  26. if isinstance(ts, pd.DataFrame):
  27. return ts
  28. elif isinstance(ts, str):
  29. ts_data = self._reader.read(ts)
  30. if ts_data is None:
  31. raise Exception(f"TS read Error: {ts}")
  32. return ts_data
  33. else:
  34. raise TypeError(
  35. f"ReadTS only supports the following types:\n"
  36. f"1. str, indicating a CSV file path or a directory containing CSV files.\n"
  37. f"2. pandas.DataFrame.\n"
  38. f"However, got type: {type(ts).__name__}."
  39. )