date_converters.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. """This module is designed for community supported date conversion functions"""
  2. import warnings
  3. import numpy as np
  4. from pandas._libs.tslibs import parsing
  5. def parse_date_time(date_col, time_col):
  6. """
  7. Parse columns with dates and times into a single datetime column.
  8. .. deprecated:: 1.2
  9. """
  10. warnings.warn(
  11. """
  12. Use pd.to_datetime(date_col + " " + time_col) instead to get a Pandas Series.
  13. Use pd.to_datetime(date_col + " " + time_col).to_pydatetime() instead to get a Numpy array.
  14. """, # noqa: E501
  15. FutureWarning,
  16. stacklevel=2,
  17. )
  18. date_col = _maybe_cast(date_col)
  19. time_col = _maybe_cast(time_col)
  20. return parsing.try_parse_date_and_time(date_col, time_col)
  21. def parse_date_fields(year_col, month_col, day_col):
  22. """
  23. Parse columns with years, months and days into a single date column.
  24. .. deprecated:: 1.2
  25. """
  26. warnings.warn(
  27. """
  28. Use pd.to_datetime({"year": year_col, "month": month_col, "day": day_col}) instead to get a Pandas Series.
  29. Use ser = pd.to_datetime({"year": year_col, "month": month_col, "day": day_col}) and
  30. np.array([s.to_pydatetime() for s in ser]) instead to get a Numpy array.
  31. """, # noqa: E501
  32. FutureWarning,
  33. stacklevel=2,
  34. )
  35. year_col = _maybe_cast(year_col)
  36. month_col = _maybe_cast(month_col)
  37. day_col = _maybe_cast(day_col)
  38. return parsing.try_parse_year_month_day(year_col, month_col, day_col)
  39. def parse_all_fields(year_col, month_col, day_col, hour_col, minute_col, second_col):
  40. """
  41. Parse columns with datetime information into a single datetime column.
  42. .. deprecated:: 1.2
  43. """
  44. warnings.warn(
  45. """
  46. Use pd.to_datetime({"year": year_col, "month": month_col, "day": day_col,
  47. "hour": hour_col, "minute": minute_col, second": second_col}) instead to get a Pandas Series.
  48. Use ser = pd.to_datetime({"year": year_col, "month": month_col, "day": day_col,
  49. "hour": hour_col, "minute": minute_col, second": second_col}) and
  50. np.array([s.to_pydatetime() for s in ser]) instead to get a Numpy array.
  51. """, # noqa: E501
  52. FutureWarning,
  53. stacklevel=2,
  54. )
  55. year_col = _maybe_cast(year_col)
  56. month_col = _maybe_cast(month_col)
  57. day_col = _maybe_cast(day_col)
  58. hour_col = _maybe_cast(hour_col)
  59. minute_col = _maybe_cast(minute_col)
  60. second_col = _maybe_cast(second_col)
  61. return parsing.try_parse_datetime_components(
  62. year_col, month_col, day_col, hour_col, minute_col, second_col
  63. )
  64. def generic_parser(parse_func, *cols):
  65. """
  66. Use dateparser to parse columns with data information into a single datetime column.
  67. .. deprecated:: 1.2
  68. """
  69. warnings.warn(
  70. """
  71. Use pd.to_datetime instead.
  72. """,
  73. FutureWarning,
  74. stacklevel=2,
  75. )
  76. N = _check_columns(cols)
  77. results = np.empty(N, dtype=object)
  78. for i in range(N):
  79. args = [c[i] for c in cols]
  80. results[i] = parse_func(*args)
  81. return results
  82. def _maybe_cast(arr):
  83. if not arr.dtype.type == np.object_:
  84. arr = np.array(arr, dtype=object)
  85. return arr
  86. def _check_columns(cols):
  87. if not len(cols):
  88. raise AssertionError("There must be at least 1 column")
  89. head, tail = cols[0], cols[1:]
  90. N = len(head)
  91. for i, n in enumerate(map(len, tail)):
  92. if n != N:
  93. raise AssertionError(
  94. f"All columns must have the same length: {N}; column {i} has length {n}"
  95. )
  96. return N