📜  Python|熊猫 DatetimeIndex.freq(1)

📅  最后修改于: 2023-12-03 15:04:26.895000             🧑  作者: Mango

Python | 熊猫 DatetimeIndex.freq

Pandas是一个强大的Python数据分析库,它为Python编写的数据架构提供了高效的、灵活的数据结构和数据操作工具,其中的DatetimeIndex是用于时间序列数据的一个常用工具。

在DatetimeIndex中,可以为时间序列数据设置频率(frequency),以指定时间戳的间隔。Pandas提供了一系列的频率设置方法,其中最常用的是freq参数。

freq参数

freq参数是一个字符串,它用于指定时间戳的间隔。具体使用方法如下:

import pandas as pd

#按年设置频率
datetimerange = pd.date_range('20220101', periods=12, freq='A')
print(datetimerange)

#按月设置频率
datetimerange = pd.date_range('20220101', periods=12, freq='M')
print(datetimerange)

输出:

DatetimeIndex(['2022-12-31', '2023-12-31', '2024-12-31', '2025-12-31',
               '2026-12-31', '2027-12-31', '2028-12-31', '2029-12-31',
               '2030-12-31', '2031-12-31', '2032-12-31', '2033-12-31'],
              dtype='datetime64[ns]', freq='A-DEC')
DatetimeIndex(['2022-01-31', '2022-02-28', '2022-03-31', '2022-04-30',
               '2022-05-31', '2022-06-30', '2022-07-31', '2022-08-31',
               '2022-09-30', '2022-10-31', '2022-11-30', '2022-12-31'],
              dtype='datetime64[ns]', freq='M')

这里有一些常用的频率及其对应的字符串:

| 代码 | 描述 | | --- | --- | | D | 每日 | | H | 每小时 | | T或min | 每分钟 | | S | 每秒钟 | | L或ms | 每毫秒 | | U或us | 每微秒 | | N | 每纳秒 | | W | 每周(以周日为第一天) | | W-MON | 每周(以周一为第一天) | | M | 每月的最后一天 | | MS | 每月的第一天 | | BM | 每月的最后一个工作日 | | BMS | 每月的第一个工作日 | | B | 每工作日 | | Q-JAN | 每季度(以1月为第一月) | | Q-FEB | 每季度(以2月为第一月) | | Q-MAR | 每季度(以3月为第一月) | | Q-APR | 每季度(以4月为第一月) | | Q-MAY | 每季度(以5月为第一月) | | Q-JUN | 每季度(以6月为第一月) | | Q-JUL | 每季度(以7月为第一月) | | Q-AUG | 每季度(以8月为第一月) | | Q-SEP | 每季度(以9月为第一月) | | Q-OCT | 每季度(以10月为第一月) | | Q-NOV | 每季度(以11月为第一月) | | Q-DEC | 每季度(以12月为第一月) | | A-JAN | 每年(以1月为第一月) | | A-FEB | 每年(以2月为第一月) | | A-MAR | 每年(以3月为第一月) | | A-APR | 每年(以4月为第一月) | | A-MAY | 每年(以5月为第一月) | | A-JUN | 每年(以6月为第一月) | | A-JUL | 每年(以7月为第一月) | | A-AUG | 每年(以8月为第一月) | | A-SEP | 每年(以9月为第一月) | | A-OCT | 每年(以10月为第一月) | | A-NOV | 每年(以11月为第一月) | | A-DEC | 每年(以12月为第一月) |

示例

以下是DatetimeIndex.freq的一些具体示例:

import pandas as pd

#按天设置频率
datetimerange = pd.date_range('20220101', periods=12, freq='D')
print(datetimerange)

#按月设置频率
datetimerange = pd.date_range('20220101', periods=12, freq='M')
print(datetimerange)

#按年设置频率
datetimerange = pd.date_range('20220101', periods=12, freq='A')
print(datetimerange)

# 每隔3分钟
datetimerange = pd.date_range('2022-01-01', periods=6, freq='3T')
print(datetimerange)

# 每隔30秒
datetimerange = pd.date_range('2022-01-01', periods=6, freq='30S')
print(datetimerange)

输出:

DatetimeIndex(['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04',
               '2022-01-05', '2022-01-06', '2022-01-07', '2022-01-08',
               '2022-01-09', '2022-01-10', '2022-01-11', '2022-01-12'],
              dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2022-01-31', '2022-02-28', '2022-03-31', '2022-04-30',
               '2022-05-31', '2022-06-30', '2022-07-31', '2022-08-31',
               '2022-09-30', '2022-10-31', '2022-11-30', '2022-12-31'],
              dtype='datetime64[ns]', freq='M')
DatetimeIndex(['2022-12-31', '2023-12-31', '2024-12-31', '2025-12-31',
               '2026-12-31', '2027-12-31', '2028-12-31', '2029-12-31',
               '2030-12-31', '2031-12-31', '2032-12-31', '2033-12-31'],
              dtype='datetime64[ns]', freq='A-DEC')
DatetimeIndex(['2022-01-01 00:00:00', '2022-01-01 00:03:00',
               '2022-01-01 00:06:00', '2022-01-01 00:09:00',
               '2022-01-01 00:12:00', '2022-01-01 00:15:00'],
              dtype='datetime64[ns]', freq='3T')
DatetimeIndex(['2022-01-01 00:00:00', '2022-01-01 00:00:30',
               '2022-01-01 00:01:00', '2022-01-01 00:01:30',
               '2022-01-01 00:02:00', '2022-01-01 00:02:30'],
              dtype='datetime64[ns]', freq='30S')

结论

Pandas的DatetimeIndex提供了灵活的、高效的时间序列数据结构,它可以根据不同的频率对时间戳的间隔进行限制,这便于程序员对不同类型的时间序列数据进行统计分析,提高代码的可读性与可维护性。