PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/pandas/tseries/util.py

http://github.com/wesm/pandas
Python | 104 lines | 81 code | 3 blank | 20 comment | 0 complexity | 2f98bf94bd9381fc667206a7fa9ff942 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. import warnings
  2. from pandas.compat import lrange
  3. import numpy as np
  4. from pandas.types.common import _ensure_platform_int
  5. from pandas.core.frame import DataFrame
  6. import pandas.core.nanops as nanops
  7. def pivot_annual(series, freq=None):
  8. """
  9. Deprecated. Use ``pivot_table`` instead.
  10. Group a series by years, taking leap years into account.
  11. The output has as many rows as distinct years in the original series,
  12. and as many columns as the length of a leap year in the units corresponding
  13. to the original frequency (366 for daily frequency, 366*24 for hourly...).
  14. The fist column of the output corresponds to Jan. 1st, 00:00:00,
  15. while the last column corresponds to Dec, 31st, 23:59:59.
  16. Entries corresponding to Feb. 29th are masked for non-leap years.
  17. For example, if the initial series has a daily frequency, the 59th column
  18. of the output always corresponds to Feb. 28th, the 61st column to Mar. 1st,
  19. and the 60th column is masked for non-leap years.
  20. With a hourly initial frequency, the (59*24)th column of the output always
  21. correspond to Feb. 28th 23:00, the (61*24)th column to Mar. 1st, 00:00, and
  22. the 24 columns between (59*24) and (61*24) are masked.
  23. If the original frequency is less than daily, the output is equivalent to
  24. ``series.convert('A', func=None)``.
  25. Parameters
  26. ----------
  27. series : Series
  28. freq : string or None, default None
  29. Returns
  30. -------
  31. annual : DataFrame
  32. """
  33. msg = "pivot_annual is deprecated. Use pivot_table instead"
  34. warnings.warn(msg, FutureWarning)
  35. index = series.index
  36. year = index.year
  37. years = nanops.unique1d(year)
  38. if freq is not None:
  39. freq = freq.upper()
  40. else:
  41. freq = series.index.freq
  42. if freq == 'D':
  43. width = 366
  44. offset = index.dayofyear - 1
  45. # adjust for leap year
  46. offset[(~isleapyear(year)) & (offset >= 59)] += 1
  47. columns = lrange(1, 367)
  48. # todo: strings like 1/1, 1/25, etc.?
  49. elif freq in ('M', 'BM'):
  50. width = 12
  51. offset = index.month - 1
  52. columns = lrange(1, 13)
  53. elif freq == 'H':
  54. width = 8784
  55. grouped = series.groupby(series.index.year)
  56. defaulted = grouped.apply(lambda x: x.reset_index(drop=True))
  57. defaulted.index = defaulted.index.droplevel(0)
  58. offset = np.asarray(defaulted.index)
  59. offset[~isleapyear(year) & (offset >= 1416)] += 24
  60. columns = lrange(1, 8785)
  61. else:
  62. raise NotImplementedError(freq)
  63. flat_index = (year - years.min()) * width + offset
  64. flat_index = _ensure_platform_int(flat_index)
  65. values = np.empty((len(years), width))
  66. values.fill(np.nan)
  67. values.put(flat_index, series.values)
  68. return DataFrame(values, index=years, columns=columns)
  69. def isleapyear(year):
  70. """
  71. Returns true if year is a leap year.
  72. Parameters
  73. ----------
  74. year : integer / sequence
  75. A given (list of) year(s).
  76. """
  77. msg = "isleapyear is deprecated. Use .is_leap_year property instead"
  78. warnings.warn(msg, FutureWarning)
  79. year = np.asarray(year)
  80. return np.logical_or(year % 400 == 0,
  81. np.logical_and(year % 4 == 0, year % 100 > 0))