ts_reader.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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 numpy as np
  15. import pandas as pd
  16. from ...utils.io import CSVReader
  17. class ReadTS:
  18. def __init__(self):
  19. super().__init__()
  20. self._reader = CSVReader(backend="pandas")
  21. def __call__(self, ts_list):
  22. """apply"""
  23. return [self.read(ts) for ts in ts_list]
  24. def read(self, ts):
  25. if isinstance(ts, pd.DataFrame):
  26. return ts
  27. elif isinstance(ts, str):
  28. ts_data = self._reader.read(ts)
  29. if ts_data is None:
  30. raise Exception(f"TS read Error: {ts}")
  31. return ts_data
  32. else:
  33. raise TypeError(
  34. f"ReadTS only supports the following types:\n"
  35. f"1. str, indicating a CSV file path or a directory containing CSV files.\n"
  36. f"2. pandas.DataFrame.\n"
  37. f"However, got type: {type(ts).__name__}."
  38. )