ts_reader.py 1.5 KB

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