pandas.to_timedelta
原文:
pandas.pydata.org/docs/reference/api/pandas.to_timedelta.html
pandas.to_timedelta(arg, unit=None, errors='raise')
将参数转换为时间差。
时间差是时间的绝对差异,以不同的单位(例如天、小时、分钟、秒)表示。此方法将从已识别的时间差格式/值中将参数转换为 Timedelta 类型。
参数:
argstr、时间差、类似列表或 Series
要转换为时间差的数据。
从 2.0 版本开始更改:带有单位‘M’、‘Y’和‘y’的字符串不代表明确的时间差值,将引发异常。
unitstr,可选
表示数值参数的单位。默认为"ns"
。
可能的值:
-
‘W’
-
‘D’ / ‘days’ / ‘day’
-
‘hours’ / ‘hour’ / ‘hr’ / ‘h’ / ‘H’
-
‘m’ / ‘minute’ / ‘min’ / ‘minutes’ / ‘T’
-
‘s’ / ‘seconds’ / ‘sec’ / ‘second’ / ‘S’
-
‘ms’ / ‘milliseconds’ / ‘millisecond’ / ‘milli’ / ‘millis’ / ‘L’
-
‘us’ / ‘microseconds’ / ‘microsecond’ / ‘micro’ / ‘micros’ / ‘U’
-
‘ns’ / ‘nanoseconds’ / ‘nano’ / ‘nanos’ / ‘nanosecond’ / ‘N’
当 arg 包含字符串且errors="raise"
时,不得指定。
自 2.2.0 版本起弃用:单位‘H’、‘T’、‘S’、‘L’、‘U’和‘N’已被弃用,并将在将来的版本中移除。请使用‘h’、‘min’、‘s’、‘ms’、‘us’和‘ns’代替‘H’、‘T’、‘S’、‘L’、‘U’和‘N’。
errors{‘ignore’, ‘raise’, ‘coerce’},默认为‘raise’
-
如果是‘raise’,那么无效的解析将引发异常。
-
如果是‘coerce’,那么无效的解析将被设置为 NaT。
-
如果是‘ignore’,那么无效的解析将返回输入。
返回:
时间差
如果解析成功。返回类型取决于输入:
-
类似列表:timedelta64 数据类型的 TimedeltaIndex
-
系列:timedelta64 数据类型的 Series
-
标量:时间差
另请参阅
DataFrame.astype
将参数转换为指定的数据类型。
to_datetime
将参数转换为日期时间。
convert_dtypes
转换数据类型。
注意
如果精度高于纳秒,对于字符串输入,持续时间的精度将被截断为纳秒。
示例
解析单个字符串为时间差:
>>> pd.to_timedelta('1 days 06:05:01.00003')
Timedelta('1 days 06:05:01.000030')
>>> pd.to_timedelta('15.5us')
Timedelta('0 days 00:00:00.000015500')
解析字符串列表或数组:
>>> pd.to_timedelta(['1 days 06:05:01.00003', '15.5us', 'nan'])
TimedeltaIndex(['1 days 06:05:01.000030', '0 days 00:00:00.000015500', NaT],
dtype='timedelta64[ns]', freq=None)
通过指定单位关键字参数转换数字:
>>> pd.to_timedelta(np.arange(5), unit='s')
TimedeltaIndex(['0 days 00:00:00', '0 days 00:00:01', '0 days 00:00:02',
'0 days 00:00:03', '0 days 00:00:04'],
dtype='timedelta64[ns]', freq=None)
>>> pd.to_timedelta(np.arange(5), unit='d')
TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'],
dtype='timedelta64[ns]', freq=None)
pandas.date_range
原文:
pandas.pydata.org/docs/reference/api/pandas.date_range.html
pandas.date_range(start=None, end=None, periods=None, freq=None, tz=None, normalize=False, name=None, inclusive='both', *, unit=None, **kwargs)
返回一个固定频率的 DatetimeIndex。
返回等间隔时间点的范围(任意两个相邻点之间的差异由给定频率指定),使得它们都满足 start <[=] x <[=] end,其中第一个和最后一个分别是该范围中落在freq
边界上的第一个和最后一个时间点(如果给定为频率字符串),或者对于freq
有效的(如果给定为pandas.tseries.offsets.DateOffset
)。 (如果start
、end
或freq
中的一个确切地没有指定,则可以根据periods
计算缺失的参数,即范围中的时间步数。请参见下面的说明。)
参数:
startstr 或日期时间样式,可选
生成日期的左边界。
endstr 或日期时间样式,可选
生成日期的右边界。
periodsint,可选
要生成的周期数。
freqstr、Timedelta、datetime.timedelta 或 DateOffset,默认为‘D’
频率字符串可以有多个,例如‘5h’。查看这里获取频率别名列表。
tzstr 或 tzinfo,可选
返回本地化的 DatetimeIndex 的时区名称,例如‘Asia/Hong_Kong’。默认情况下,生成的 DatetimeIndex 是时区无关的,除非传递了时区感知的日期时间。
normalizebool,默认为 False
在生成日期范围之前将开始/结束日期标准化为午夜。
namestr,默认为 None
生成的 DatetimeIndex 的名称。
inclusive{“both”, “neither”, “left”, “right”},默认为“both”
包括边界;是否将每个边界设置为闭合或开放。
在版本 1.4.0 中新增。
unitstr,默认为 None
指定结果的期望分辨率。
在版本 2.0.0 中新增。
**kwargs
用于兼容性。对结果没有影响。
返回:
DatetimeIndex
另请参阅
DatetimeIndex
一个不可变的日期时间容器。
timedelta_range
返回一个固定频率的 TimedeltaIndex。
period_range
返回一个固定频率的 PeriodIndex。
interval_range
返回一个固定频率的 IntervalIndex。
注意
在四个参数start
、end
、periods
和freq
中,必须指定三个。如果省略了freq
,则生成的DatetimeIndex
将在start
和end
之间(两侧均闭合)具有periods
个线性间隔的元素。
欲了解更多关于频率字符串的信息,请查看此链接。
示例
指定值
下面的四个示例生成相同的 DatetimeIndex,但变化了开始、结束和周期的组合。
指定开始和结束,默认为每日频率。
>>> pd.date_range(start='1/1/2018', end='1/08/2018')
DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04',
'2018-01-05', '2018-01-06', '2018-01-07', '2018-01-08'],
dtype='datetime64[ns]', freq='D')
指定时区感知的开始和结束,默认为每日频率。
>>> pd.date_range(
... start=pd.to_datetime("1/1/2018").tz_localize("Europe/Berlin"),
... end=pd.to_datetime("1/08/2018").tz_localize("Europe/Berlin"),
... )
DatetimeIndex(['2018-01-01 00:00:00+01:00', '2018-01-02 00:00:00+01:00',
'2018-01-03 00:00:00+01:00', '2018-01-04 00:00:00+01:00',
'2018-01-05 00:00:00+01:00', '2018-01-06 00:00:00+01:00',
'2018-01-07 00:00:00+01:00', '2018-01-08 00:00:00+01:00'],
dtype='datetime64[ns, Europe/Berlin]', freq='D')
指定开始和周期,周期数(天数)。
>>> pd.date_range(start='1/1/2018', periods=8)
DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04',
'2018-01-05', '2018-01-06', '2018-01-07', '2018-01-08'],
dtype='datetime64[ns]', freq='D')
指定结束和周期,周期数(天数)。
>>> pd.date_range(end='1/1/2018', periods=8)
DatetimeIndex(['2017-12-25', '2017-12-26', '2017-12-27', '2017-12-28',
'2017-12-29', '2017-12-30', '2017-12-31', '2018-01-01'],
dtype='datetime64[ns]', freq='D')
指定开始、结束和周期;频率将自动生成(线性间隔)。
>>> pd.date_range(start='2018-04-24', end='2018-04-27', periods=3)
DatetimeIndex(['2018-04-24 00:00:00', '2018-04-25 12:00:00',
'2018-04-27 00:00:00'],
dtype='datetime64[ns]', freq=None)
其他参数
将 freq(频率)更改为 'ME'
(月末频率)。
>>> pd.date_range(start='1/1/2018', periods=5, freq='ME')
DatetimeIndex(['2018-01-31', '2018-02-28', '2018-03-31', '2018-04-30',
'2018-05-31'],
dtype='datetime64[ns]', freq='ME')
允许使用倍数。
>>> pd.date_range(start='1/1/2018', periods=5, freq='3ME')
DatetimeIndex(['2018-01-31', '2018-04-30', '2018-07-31', '2018-10-31',
'2019-01-31'],
dtype='datetime64[ns]', freq='3ME')
freq 也可以指定为一个 Offset 对象。
>>> pd.date_range(start='1/1/2018', periods=5, freq=pd.offsets.MonthEnd(3))
DatetimeIndex(['2018-01-31', '2018-04-30', '2018-07-31', '2018-10-31',
'2019-01-31'],
dtype='datetime64[ns]', freq='3ME')
指定时区以设置时区。
>>> pd.date_range(start='1/1/2018', periods=5, tz='Asia/Tokyo')
DatetimeIndex(['2018-01-01 00:00:00+09:00', '2018-01-02 00:00:00+09:00',
'2018-01-03 00:00:00+09:00', '2018-01-04 00:00:00+09:00',
'2018-01-05 00:00:00+09:00'],
dtype='datetime64[ns, Asia/Tokyo]', freq='D')
inclusive 控制是否包括位于边界上的开始和结束。默认值“both”包括两端的边界点。
>>> pd.date_range(start='2017-01-01', end='2017-01-04', inclusive="both")
DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04'],
dtype='datetime64[ns]', freq='D')
使用 inclusive='left'
来排除结束如果它在边界上的情况。
>>> pd.date_range(start='2017-01-01', end='2017-01-04', inclusive='left')
DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03'],
dtype='datetime64[ns]', freq='D')
使用 inclusive='right'
来排除开始如果它在边界上的情况,类似地,inclusive='neither'
将同时排除开始和结束。
>>> pd.date_range(start='2017-01-01', end='2017-01-04', inclusive='right')
DatetimeIndex(['2017-01-02', '2017-01-03', '2017-01-04'],
dtype='datetime64[ns]', freq='D')
指定单位
>>> pd.date_range(start="2017-01-01", periods=10, freq="100YS", unit="s")
DatetimeIndex(['2017-01-01', '2117-01-01', '2217-01-01', '2317-01-01',
'2417-01-01', '2517-01-01', '2617-01-01', '2717-01-01',
'2817-01-01', '2917-01-01'],
dtype='datetime64[s]', freq='100YS-JAN')
pandas.bdate_range
原文:
pandas.pydata.org/docs/reference/api/pandas.bdate_range.html
pandas.bdate_range(start=None, end=None, periods=None, freq='B', tz=None, normalize=True, name=None, weekmask=None, holidays=None, inclusive='both', **kwargs)
返回一个以工作日为默认的固定频率 DatetimeIndex。
参数:
start字符串或类似 datetime 的对象,默认为 None
生成日期的左边界。
end字符串或类似 datetime 的对象,默认为 None
生成日期的右边界。
periods整数,默认为 None
要生成的周期数。
freq字符串,Timedelta,datetime.timedelta 或 DateOffset,默认为‘B’
频率字符串可以有多个,例如‘5h’。默认为工作日(‘B’)。
tz字符串或 None
返回本地化 DatetimeIndex 的时区名称,例如 Asia/Beijing。
normalize布尔值,默认为 False
在生成日期范围之前,将开始/结束日期标准化为午夜。
name字符串,默认为 None
结果 DatetimeIndex 的名称。
weekmask字符串或 None,默认为 None
有效工作日的周掩码,传递给numpy.busdaycalendar
,仅在传递自定义频率字符串时使用。默认值 None 等同于‘Mon Tue Wed Thu Fri’。
holidays列表或 None,默认为 None
要从有效工作日集中排除的日期,传递给numpy.busdaycalendar
,仅在传递自定义频率字符串时使用。
inclusive{“both”, “neither”, “left”, “right”},默认为“both”
包括边界;是否将每个边界设置为闭合或开放。
版本 1.4.0 中的新功能。
**kwargs
为了兼容性。对结果没有影响。
返回:
DatetimeIndex
注意
在四个参数中:start
、end
、periods
和freq
,必须指定三个。对于bdate_range
,指定freq
是必需的。如果不希望指定freq
,请使用date_range
。
要了解更多关于频率字符串的信息,请查看此链接。
示例
注意结果中如何跳过两个周末日。
>>> pd.bdate_range(start='1/1/2018', end='1/08/2018')
DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04',
'2018-01-05', '2018-01-08'],
dtype='datetime64[ns]', freq='B')
pandas.period_range
原文:
pandas.pydata.org/docs/reference/api/pandas.period_range.html
pandas.period_range(start=None, end=None, periods=None, freq=None, name=None)
返回一个固定频率的 PeriodIndex。
默认频率为日历日。
参数:
startstr、datetime、date、pandas.Timestamp 或 period-like,默认为 None
生成周期的左边界。
endstr、datetime、date、pandas.Timestamp 或 period-like,默认为 None
生成周期的右边界。
periodsint,默认为 None
要生成的周期数。
freqstr 或 DateOffset,可选
频率别名。 默认情况下,如果 start
或 end
是 Period 对象,则从中获取 freq。 否则,默认为每日频率 "D"
。
namestr,默认为 None
结果 PeriodIndex 的名称。
返回:
PeriodIndex
注意事项
在三个参数 start
、end
和 periods
中,必须指定两个。
要了解更多有关频率字符串的信息,请参阅此链接。
示例
>>> pd.period_range(start='2017-01-01', end='2018-01-01', freq='M')
PeriodIndex(['2017-01', '2017-02', '2017-03', '2017-04', '2017-05', '2017-06',
'2017-07', '2017-08', '2017-09', '2017-10', '2017-11', '2017-12',
'2018-01'],
dtype='period[M]')
如果 start
或 end
是 Period
对象,则它们将用作与 period_range
构造函数匹配频率的锚定端点的 PeriodIndex
。
>>> pd.period_range(start=pd.Period('2017Q1', freq='Q'),
... end=pd.Period('2017Q2', freq='Q'), freq='M')
PeriodIndex(['2017-03', '2017-04', '2017-05', '2017-06'],
dtype='period[M]')
pandas.timedelta_range
原文:
pandas.pydata.org/docs/reference/api/pandas.timedelta_range.html
pandas.timedelta_range(start=None, end=None, periods=None, freq=None, name=None, closed=None, *, unit=None)
返回一个以天为默认值的固定频率 TimedeltaIndex。
参数:
startstr 或类似时间增量,默认为 None
生成时间增量的左边界。
endstr 或类似时间增量,默认为 None
生成时间增量的右边界。
periodsint,默认为 None
要生成的周期数。
freqstr、Timedelta、datetime.timedelta 或 DateOffset,默认为‘D’
频率字符串可以有多个,例如‘5h’。
namestr,默认为 None
结果 TimedeltaIndex 的名称。
closedstr,默认为 None
使间隔相对于给定频率在‘左’、‘右’或两侧(None)上闭合。
unitstr,默认为 None
指定结果的所需分辨率。
在 2.0.0 版本中新增。
返回:
TimedeltaIndex
注意
在四个参数start
、end
、periods
和freq
中,必须指定三个。如果省略freq
,则生成的TimedeltaIndex
将在start
和end
之间(两侧都闭合)具有periods
个线性间隔的元素。
要了解更多关于频率字符串的信息,请参见此链接。
示例
>>> pd.timedelta_range(start='1 day', periods=4)
TimedeltaIndex(['1 days', '2 days', '3 days', '4 days'],
dtype='timedelta64[ns]', freq='D')
closed
参数指定包含哪个端点。默认行为是包含两个端点。
>>> pd.timedelta_range(start='1 day', periods=4, closed='right')
TimedeltaIndex(['2 days', '3 days', '4 days'],
dtype='timedelta64[ns]', freq='D')
freq
参数指定 TimedeltaIndex 的频率。只能传递固定频率,非固定频率如‘M’(月末)会引发错误。
>>> pd.timedelta_range(start='1 day', end='2 days', freq='6h')
TimedeltaIndex(['1 days 00:00:00', '1 days 06:00:00', '1 days 12:00:00',
'1 days 18:00:00', '2 days 00:00:00'],
dtype='timedelta64[ns]', freq='6h')
指定start
、end
和periods
;频率将自动生成(线性间隔)。
>>> pd.timedelta_range(start='1 day', end='5 days', periods=4)
TimedeltaIndex(['1 days 00:00:00', '2 days 08:00:00', '3 days 16:00:00',
'5 days 00:00:00'],
dtype='timedelta64[ns]', freq=None)
指定一个单位
>>> pd.timedelta_range("1 Day", periods=3, freq="100000D", unit="s")
TimedeltaIndex(['1 days', '100001 days', '200001 days'],
dtype='timedelta64[s]', freq='100000D')
pandas.infer_freq
原文:
pandas.pydata.org/docs/reference/api/pandas.infer_freq.html
pandas.infer_freq(index)
推断给定输入索引的最可能频率。
参数:
索引:DatetimeIndex、TimedeltaIndex、Series 或者类似数组
如果传递了一个 Series,将使用该系列的值(而不是索引)。
返回:
字符串或者 None
如果没有明显的频率,则为 None。
引发:
TypeError
如果索引不是类似于日期时间的。
ValueError
如果值少于三个。
示例
>>> idx = pd.date_range(start='2020/12/01', end='2020/12/30', periods=30)
>>> pd.infer_freq(idx)
'D'
pandas.interval_range
原文:
pandas.pydata.org/docs/reference/api/pandas.interval_range.html
pandas.interval_range(start=None, end=None, periods=None, freq=None, name=None, closed='right')
返回一个固定频率的 IntervalIndex。
参数:
start数值或类似日期时间,默认为 None
生成区间的左边界。
end数值或类似日期时间,默认为 None
生成区间的右边界。
periodsint,默认为 None
要生成的周期数。
freq数值、字符串、Timedelta、datetime.timedelta 或 DateOffset,默认为 None
每个区间的长度。必须与 start 和 end 的类型一致,例如数值为 2,或者类似日期时间为‘5H’。数值类型默认为 1,类似日期时间默认为‘D’。
name字符串,默认为 None
结果 IntervalIndex 的名称。
closed{‘left’, ‘right’, ‘both’, ‘neither’},默认为‘right’
区间是在左侧、右侧、两侧还是无一侧闭合。
返回:
区间索引
另请参阅
IntervalIndex
一个在同一侧都是闭合的区间索引。
注意
四个参数start
、end
、periods
和freq
中,必须指定三个。如果省略了freq
,则生成的IntervalIndex
将在start
和end
之间(包括两端)均匀间隔periods
个元素。
要了解更多关于类似日期时间频率字符串的信息,请参阅此链接。
示例
支持数值类型的start
和end
。
>>> pd.interval_range(start=0, end=5)
IntervalIndex([(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]],
dtype='interval[int64, right]')
此外,也支持类似日期时间的输入。
>>> pd.interval_range(start=pd.Timestamp('2017-01-01'),
... end=pd.Timestamp('2017-01-04'))
IntervalIndex([(2017-01-01 00:00:00, 2017-01-02 00:00:00],
(2017-01-02 00:00:00, 2017-01-03 00:00:00],
(2017-01-03 00:00:00, 2017-01-04 00:00:00]],
dtype='interval[datetime64[ns], right]')
freq
参数指定 IntervalIndex 内各个区间的左右端点之间的频率。对于数值类型的start
和end
,频率也必须是数值类型。
>>> pd.interval_range(start=0, periods=4, freq=1.5)
IntervalIndex([(0.0, 1.5], (1.5, 3.0], (3.0, 4.5], (4.5, 6.0]],
dtype='interval[float64, right]')
同样,对于类似日期时间的start
和end
,频率必须可以转换为 DateOffset。
>>> pd.interval_range(start=pd.Timestamp('2017-01-01'),
... periods=3, freq='MS')
IntervalIndex([(2017-01-01 00:00:00, 2017-02-01 00:00:00],
(2017-02-01 00:00:00, 2017-03-01 00:00:00],
(2017-03-01 00:00:00, 2017-04-01 00:00:00]],
dtype='interval[datetime64[ns], right]')
指定start
、end
和periods
;频率将自动生成(均匀间隔)。
>>> pd.interval_range(start=0, end=6, periods=4)
IntervalIndex([(0.0, 1.5], (1.5, 3.0], (3.0, 4.5], (4.5, 6.0]],
dtype='interval[float64, right]')
closed
参数指定 IntervalIndex 内各个区间的端点是闭合的哪一侧。
>>> pd.interval_range(end=5, periods=4, closed='both')
IntervalIndex([[1, 2], [2, 3], [3, 4], [4, 5]],
dtype='interval[int64, both]')
pandas.eval
pandas.eval(expr, parser='pandas', engine=None, local_dict=None, global_dict=None, resolvers=(), level=0, target=None, inplace=False)
使用不同后端以字符串形式评估 Python 表达式。
支持以下算术运算:+
、-
、*
、/
、**
、%
、//
(仅限 python 引擎),以及以下布尔运算:|
(或)、&
(与)和 ~
(非)。此外,‘pandas’ 解析器允许使用 and
、or
和 not
,其语义与相应的位运算符相同。支持 Series
和 DataFrame
对象,并且表现方式与普通的 Python 评估相同。
参数:
exprstr
要评估的表达式。此字符串不能包含任何 Python 语句,只能包含 Python 表达式。
parser{‘pandas’、‘python’},默认为 ‘pandas’
用于从表达式构造语法树的解析器。默认值为 'pandas'
,与标准 Python 稍有不同。或者,您可以使用 'python'
解析器解析表达式以保留严格的 Python 语义。有关更多详细信息,请参阅增强性能文档。
engine{‘python’、‘numexpr’}��默认为 ‘numexpr’
用于评估表达式的引擎。支持的引擎有
-
None:尝试使用
numexpr
,如果失败则退回到python
-
'numexpr'
:此默认引擎使用 numexpr 评估 pandas 对象,可大幅提高具有大型帧的复杂表达式的速度。 -
'python'
:执行操作,就好像在顶层 Python 中执行了eval
。此引擎通常不太有用。
未来可能会提供更多后端。
local_dictdict 或 None,可选
本地变量字典,默认为 locals()。
global_dictdict 或 None,可选
全局变量字典,默认为 globals()。
resolversdict-like 列表或 None,可选
一个实现 __getitem__
特殊方法的对象列表,您可以使用它们来注入额外的命名空间集合,以用于变量查找。例如,在 query()
方法中使用它来注入 DataFrame.index
和 DataFrame.columns
变量,这些变量分别指代它们各自的 DataFrame
实例属性。
levelint,可选
要遍历并添加到当前作用域的先前堆栈帧数。大多数用户不需要更改此参数。
目标对象,可选,默认为 None
这是用于赋值的目标对象。当表达式中有变量赋值时使用。如果是这样,那么目标必须支持使用字符串键进行项目分配,并且如果返回一个副本,则它还必须支持.copy()
。
inplace布尔值,默认为 False
如果提供了目标对象,并且表达式会改变目标对象,则决定是否就地修改目标对象。否则,返回带有变异的目标对象的副本。
返回:
ndarray、数值标量、DataFrame、Series 或 None
评估给定代码的完成值,如果inplace=True
则为 None。
引发:
ValueError
有许多情况会引发此类错误:
-
目标为 None,但表达式是多行的。
-
表达式是多行的,但并非所有都有项目分配。一个这样排列的示例是:
a = b + 1 a + 2
这里有不同行上的表达式,使其成为多行,但最后一行没有将 a + 2 的输出分配给任何变量。
-
inplace=True,但表达式缺少项目分配。
-
提供了项目分配,但目标不支持字符串项目分配。
-
提供了项目分配且 inplace=False,但目标不支持
.copy()
方法
另请参见
DataFrame.query
评估布尔表达式以查询帧的列。
DataFrame.eval
评估描述 DataFrame 列操作的字符串。
注意
参与算术%
操作的任何对象的dtype
都会递归转换为float64
。
有关更多详细信息,请参阅提高性能文档。
示例
>>> df = pd.DataFrame({"animal": ["dog", "pig"], "age": [10, 20]})
>>> df
animal age
0 dog 10
1 pig 20
我们可以使用pd.eval
添加一个新列:
>>> pd.eval("double_age = df.age * 2", target=df)
animal age double_age
0 dog 10 20
1 pig 20 40
pandas.tseries.api.guess_datetime_format
原文:
pandas.pydata.org/docs/reference/api/pandas.tseries.api.guess_datetime_format.html
pandas.tseries.api.guess_datetime_format(dt_str, dayfirst=False)
猜测给定日期时间字符串的日期时间格式。
参数:
dt_strstr
要猜测格式的日期时间字符串。
dayfirstbool,默认为 False
如果为 True,则解析日期时以日期为首,例如 20/01/2005
警告
dayfirst=True 不是严格的,但会倾向于首先解析日期(这是一个已知的 bug)。
返回:
str or Noneret
日期时间格式字符串(用于 strftime 或 strptime),如果无法猜测则为 None。
示例
>>> from pandas.tseries.api import guess_datetime_format
>>> guess_datetime_format('09/13/2023')
'%m/%d/%Y'
>>> guess_datetime_format('2023|September|13')
pandas.util.hash_array
原文:
pandas.pydata.org/docs/reference/api/pandas.util.hash_array.html
pandas.util.hash_array(vals, encoding='utf8', hash_key='0123456789123456', categorize=True)
给定一个一维数组,返回一个确定性整数数组。
参数:
valsndarray 或 ExtensionArray
encodingstr,默认为’utf8’
字符串数据和键的编码。
hash_keystr,默认为 _default_hash_key
用于编码字符串键的哈希键。
categorizebool,默认为 True
是否在哈希之前先对对象数组进行分类。当数组包含重复值时,这样做更有效率。
返回值:
ndarray[np.uint64, ndim=1]
哈希值,与 vals 长度相同。
示例
>>> pd.util.hash_array(np.array([1, 2, 3]))
array([ 6238072747940578789, 15839785061582574730, 2185194620014831856],
dtype=uint64)
pandas.util.hash_pandas_object
原文:
pandas.pydata.org/docs/reference/api/pandas.util.hash_pandas_object.html
pandas.util.hash_pandas_object(obj, index=True, encoding='utf8', hash_key='0123456789123456', categorize=True)
返回一个索引/系列/数据帧的数据哈希。
参数:
obj索引、系列或数据帧
indexbool,默认为 True
在哈希中包含索引(如果是系列/数据帧)。
encodingstr,默认为’utf8’
当字符串时,数据和键的编码。
hash_keystr,默认为 _default_hash_key
用于编码字符串键的哈希键。
categorizebool,默认为 True
是否在哈希之前首先对对象数组进行分类。当数组包含重复值时,这样更有效率。
返回:
与对象长度相同的 uint64 系列
示例
>>> pd.util.hash_pandas_object(pd.Series([1, 2, 3]))
0 14639053686158035780
1 3869563279212530728
2 393322362522515241
dtype: uint64
pandas.api.interchange.from_dataframe
原文:
pandas.pydata.org/docs/reference/api/pandas.api.interchange.from_dataframe.html
pandas.api.interchange.from_dataframe(df, allow_copy=True)
从支持交换协议的任何 DataFrame 构建pd.DataFrame
。
参数:
dfDataFrameXchg
支持交换协议的对象,即 dataframe 方法。
allow_copybool,默认值:True
是否允许复制内存以执行转换(如果为 false,则请求零拷贝方法)。
返回:
pd.DataFrame
示例
>>> df_not_necessarily_pandas = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
>>> interchange_object = df_not_necessarily_pandas.__dataframe__()
>>> interchange_object.column_names()
Index(['A', 'B'], dtype='object')
>>> df_pandas = (pd.api.interchange.from_dataframe
... (interchange_object.select_columns_by_name(['A'])))
>>> df_pandas
A
0 1
1 2
这些方法(column_names
,select_columns_by_name
)应该适用于任何实现交换协议的数据框库。
Series
构造函数
Series ([data, index, dtype, name, copy, …]) | 具有轴标签(包括时间序列)的一维 ndarray。 |
---|
属性
轴
Series.index | Series 的索引(轴标签)。 |
---|---|
Series.array | 支持此 Series 或 Index 的数据的 ExtensionArray。 |
Series.values | 返回 Series 作为 ndarray 或者类似 ndarray,取决于 dtype。 |
Series.dtype | 返回底层数据的 dtype 对象。 |
Series.shape | 返回底层数据的形状的元组。 |
Series.nbytes | 返回底层数据中的字节数。 |
Series.ndim | 底层数据的维度数量,根据定义为 1。 |
Series.size | 返回底层数据中的元素数量。 |
Series.T | 返回转置,根据定义是自身。 |
Series.memory_usage ([index, deep]) | 返回 Series 的内存使用情况。 |
Series.hasnans | 如果存在任何 NaN,则返回 True。 |
Series.empty | 指示 Series/DataFrame 是否为空。 |
Series.dtypes | 返回底层数据的 dtype 对象。 |
Series.name | 返回 Series 的名称。 |
Series.flags | 获取与此 pandas 对象关联的属性。 |
Series.set_flags (*[, copy, …]) | 返回具有更新标志的新对象。 |
转换
Series.astype (dtype[, copy, errors]) | 将 pandas 对象转换为指定的 dtype dtype 。 |
---|---|
Series.convert_dtypes ([infer_objects, …]) | 使用支持pd.NA 的 dtype 将列转换为最佳可能的 dtype。 |
Series.infer_objects ([copy]) | 尝试为对象列推断更好的 dtype。 |
Series.copy ([deep]) | 复制此对象的索引和数据。 |
Series.bool () | (已弃用)返回单个元素 Series 或 DataFrame 的布尔值。 |
Series.to_numpy ([dtype, copy, na_value]) | 表示此 Series 或 Index 中的值的 NumPy ndarray。 |
Series.to_period ([freq, copy]) | 将 Series 从 DatetimeIndex 转换为 PeriodIndex。 |
Series.to_timestamp ([freq, how, copy]) | 转换为时间戳的 DatetimeIndex,位于周期的开始。 |
Series.to_list () | 返回值的列表。 |
Series.__array__ ([dtype, copy]) | 将值作为 NumPy 数组返回。 |
索引,迭代
Series.get (key[, default]) | 获取给定键(例如 DataFrame 列)的对象中的项目。 |
---|---|
Series.at | 访问行/列标签对的单个值。 |
Series.iat | 通过整数位置访问行/列对的单个值。 |
Series.loc | 通过标签或布尔数组访问一组行和列。 |
Series.iloc | (已弃用)基于纯整数位置的索引,按位置选择。 |
Series.__iter__ () | 返回值的迭代器。 |
Series.items () | 惰性地遍历(索引,值)元组。 |
Series.keys () | 返回索引的别名。 |
Series.pop (item) | 返回项目并从系列中删除。 |
Series.item () | 将底层数据的第一个元素作为 Python 标量返回。 |
Series.xs (key[, axis, level, drop_level]) | 从系列/数据框返回横截面。 |
更多关于 .at
, .iat
, .loc
, 和 .iloc
的信息,请参阅索引文档。
二元运算符函数
Series.add (other[, level, fill_value, axis]) | 返回系列和其他元素的加法(二元运算符 add)。 |
---|---|
Series.sub (other[, level, fill_value, axis]) | 返回系列和其他元素的减法(二元运算符 sub)。 |
Series.mul (other[, level, fill_value, axis]) | 返回系列和其他元素的乘法(二元运算符 mul)。 |
Series.div (other[, level, fill_value, axis]) | 返回系列和其他元素的浮点除法(二元运算符 truediv)。 |
Series.truediv (other[, level, fill_value, axis]) | 返回系列和其他元素的浮点除法(二元运算符 truediv)。 |
Series.floordiv (other[, level, fill_value, axis]) | 返回系列和其他元素的整数除法(二元运算符 floordiv)。 |
Series.mod (other[, level, fill_value, axis]) | 返回系列和其他元素的模运算(二元运算符 mod)。 |
Series.pow (other[, level, fill_value, axis]) | 返回系列和其他元素的指数幂运算(二元运算符 pow)。 |
Series.radd (other[, level, fill_value, axis]) | 返回系列和其他元素的加法(二元运算符 radd)。 |
Series.rsub (other[, level, fill_value, axis]) | 返回系列和其他元素的减法(二元运算符 rsub)。 |
Series.rmul (other[, level, fill_value, axis]) | 返回系列和其他元素的乘法(二元运算符 rmul)。 |
Series.rdiv (other[, level, fill_value, axis]) | 返回系列和其他元素的浮点除法(二元运算符 rtruediv)。 |
Series.rtruediv (other[, level, fill_value, axis]) | 返回 Series 和其他的浮点除法,逐元素进行(二元运算符 rtruediv)。 |
Series.rfloordiv (other[, level, fill_value, …]) | 返回 Series 和其他的整数除法,逐元素进行(二元运算符 rfloordiv)。 |
Series.rmod (other[, level, fill_value, axis]) | 返回 Series 和其他的模运算,逐元素进行(二元运算符 rmod)。 |
Series.rpow (other[, level, fill_value, axis]) | 返回 Series 和其他的指数幂,逐元素进行(二元运算符 rpow)。 |
Series.combine (other, func[, fill_value]) | 根据 func 将 Series 与 Series 或标量组合。 |
Series.combine_first (other) | 用 ‘other’ 中相同位置的值更新空元素。 |
Series.round ([decimals]) | 将 Series 中的每个值四舍五入到给定的小数位数。 |
Series.lt (other[, level, fill_value, axis]) | 返回 Series 和其他的小于关系,逐元素进行(二元运算符 lt)。 |
Series.gt (other[, level, fill_value, axis]) | 返回 Series 和其他的大于关系,逐元素进行(二元运算符 gt)。 |
Series.le (other[, level, fill_value, axis]) | 返回 Series 和其他的小于或等于关系,逐元素进行(二元运算符 le)。 |
Series.ge (other[, level, fill_value, axis]) | 返回 Series 和其他的大于或等于关系,逐元素进行(二元运算符 ge)。 |
Series.ne (other[, level, fill_value, axis]) | 返回 Series 和其他的不等于关系,逐元素进行(二元运算符 ne)。 |
Series.eq (other[, level, fill_value, axis]) | 返回 Series 和其他的等于关系,逐元素进行(二元运算符 eq)。 |
Series.product ([axis, skipna, numeric_only, …]) | 返回请求轴上值的乘积。 |
Series.dot (other) | 计算 Series 和其他列之间的点积。 |
函数应用、GroupBy 和窗口
Series.apply (func[, convert_dtype, args, by_row]) | 在 Series 的值上调用函数。 |
---|---|
Series.agg ([func, axis]) | 使用一个或多个操作聚合指定轴上的数据。 |
Series.aggregate ([func, axis]) | 使用一个或多个操作聚合指定轴上的数据。 |
Series.transform (func[, axis]) | 对自身调用 func ,产生与自身轴形状相同的 Series。 |
Series.map (arg[, na_action]) | 根据输入的映射或函数映射 Series 的值。 |
Series.groupby ([by, axis, level, as_index, …]) | 使用映射器或列的 Series 进行分组。 |
Series.rolling (window[, min_periods, …]) | 提供滚动窗口计算。 |
Series.expanding ([min_periods, axis, method]) | 提供扩展窗口计算。 |
Series.ewm ([com, span, halflife, alpha, …]) | 提供指数加权 (EW) 计算。 |
Series.pipe (func, *args, **kwargs) | 应用可链式调用的函数,期望 Series 或 DataFrame 作为输入。 |
计算 / 描述统计
Series.abs () | 返回具有每个元素的绝对数值的 Series/DataFrame。 |
---|---|
Series.all ([axis, bool_only, skipna]) | 返回是否所有元素均为 True,可能沿着轴进行计算。 |
Series.any (*[, axis, bool_only, skipna]) | 返回是否有任何元素为 True,可能沿着轴进行计算。 |
Series.autocorr ([lag]) | 计算滞后 N 的自相关。 |
Series.between (left, right[, inclusive]) | 返回等同于 left <= series <= right 的布尔 Series。 |
Series.clip ([lower, upper, axis, inplace]) | 在输入阈值处修剪值。 |
Series.corr (other[, method, min_periods]) | 计算与其他 Series 的相关性,不包括缺失值。 |
Series.count () | 返回 Series 中非 NA/null 观测值的数量。 |
Series.cov (other[, min_periods, ddof]) | 计算与 Series 的协方差,不包括缺失值。 |
Series.cummax ([axis, skipna]) | 返回 DataFrame 或 Series 轴上的累积最大值。 |
Series.cummin ([axis, skipna]) | 返回 DataFrame 或 Series 轴上的累积最小值。 |
Series.cumprod ([axis, skipna]) | 返回 DataFrame 或 Series 轴上的累积乘积。 |
Series.cumsum ([axis, skipna]) | 返回 DataFrame 或 Series 轴上的累积和。 |
Series.describe ([percentiles, include, exclude]) | 生成描述性统计信息。 |
Series.diff ([periods]) | 元素的第一个离散差异。 |
Series.factorize ([sort, use_na_sentinel]) | 将对象编码为枚举类型或分类变量。 |
Series.kurt ([axis, skipna, numeric_only]) | 返回请求轴上的无偏峰度。 |
Series.max ([axis, skipna, numeric_only]) | 返回请求轴上的最大值。 |
Series.mean ([axis, skipna, numeric_only]) | 返回请求轴上的均值。 |
Series.median ([axis, skipna, numeric_only]) | 返回请求轴上的中位数。 |
Series.min ([axis, skipna, numeric_only]) | 返回请求轴上的最小值。 |
Series.mode ([dropna]) | 返回 Series 的众数。 |
Series.nlargest ([n, keep]) | 返回最大的 n 个元素。 |
Series.nsmallest ([n, keep]) | 返回最小的 n 个元素。 |
Series.pct_change ([periods, fill_method, …]) | 当前元素与先前元素之间的分数变化。 |
Series.prod ([axis, skipna, numeric_only, …]) | 返回请求轴上的值的乘积。 |
Series.quantile ([q, interpolation]) | 返回给定分位数处的值。 |
Series.rank ([axis, method, numeric_only, …]) | 沿轴计算数值数据排名(1 到 n)。 |
Series.sem ([axis, skipna, ddof, numeric_only]) | 返回请求轴上的均值的无偏标准误差。 |
Series.skew ([axis, skipna, numeric_only]) | 返回请求轴上的无偏斜度。 |
Series.std ([axis, skipna, ddof, numeric_only]) | 返回请求轴上的样本标准差。 |
Series.sum ([axis, skipna, numeric_only, …]) | 返回请求轴上的值的总和。 |
Series.var ([axis, skipna, ddof, numeric_only]) | 返回请求轴上的无偏方差。 |
Series.kurtosis ([axis, skipna, numeric_only]) | 返回请求轴上的无偏��度。 |
Series.unique () | 返回 Series 对象的唯一值。 |
Series.nunique ([dropna]) | 返回对象中唯一元素的数量。 |
Series.is_unique | 如果对象中的值是唯一的,则返回布尔值。 |
Series.is_monotonic_increasing | 如果对象中的值单调递增,则返回布尔值。 |
Series.is_monotonic_decreasing | 如果对象中的值单调递减,则返回布尔值。 |
Series.value_counts ([normalize, sort, …]) | 返回包含唯一值计数的 Series。 |
重新索引/选择/标签操作
Series.align (other[, join, axis, level, …]) | 使用指定的连接方法在它们的轴上对齐两个对象。 |
---|---|
Series.case_when (caselist) | 替换条件为 True 的值。 |
Series.drop ([labels, axis, index, columns, …]) | 返回删除指定索引标签的 Series。 |
Series.droplevel (level[, axis]) | 返回删除请求的索引/列级别的 Series/DataFrame。 |
Series.drop_duplicates (*[, keep, inplace, …]) | 返回删除重复值的 Series。 |
Series.duplicated ([keep]) | 指示重复的 Series 值。 |
Series.equals (other) | 测试两个对象是否包含相同的元素。 |
Series.first (offset) | (已弃用) 根据日期偏移量选择时间序列数据的初始周期。 |
Series.head ([n]) | 返回前 n 行。 |
Series.idxmax ([axis, skipna]) | 返回最大值的行标签。 |
Series.idxmin ([axis, skipna]) | 返回最小值的行标签。 |
Series.isin (values) | Series 中的元素是否包含在 values 中。 |
Series.last (offset) | (已弃用) 根据日期偏移量选择时间序列数据的最终周期。 |
Series.reindex ([index, axis, method, copy, …]) | 将 Series 调整为具有新索引的对象,并可选择填充逻辑。 |
Series.reindex_like (other[, method, copy, …]) | 返回具有与其他对象匹配索引的对象。 |
Series.rename ([index, axis, copy, inplace, …]) | 修改 Series 的索引标签或名称。 |
Series.rename_axis ([mapper, index, axis, …]) | 为索引或列设置轴的名称。 |
Series.reset_index ([level, drop, name, …]) | 生成一个重置索引的新 DataFrame 或 Series。 |
Series.sample ([n, frac, replace, weights, …]) | 从对象的一个轴中返回随机抽样的项目。 |
Series.set_axis (labels, *[, axis, copy]) | 为给定轴分配所需的索引。 |
Series.take (indices[, axis]) | 沿着一个轴返回给定 位置 索引处的元素。 |
Series.tail ([n]) | 返回最后 n 行。 |
Series.truncate ([before, after, axis, copy]) | 截断 Series 或 DataFrame 在某个索引值之前和之后。 |
Series.where (cond[, other, inplace, axis, level]) | 替换条件为 False 的值。 |
Series.mask (cond[, other, inplace, axis, level]) | 替换条件为 True 的值。 |
Series.add_prefix (prefix[, axis]) | 用字符串前缀添加标签。 |
Series.add_suffix (suffix[, axis]) | 用字符串后缀添加标签。 |
Series.filter ([items, like, regex, axis]) | 根据指定的索引标签对数据帧的行或列进行子集筛选。 |
缺失数据处理
Series.backfill (*[, axis, inplace, limit, …]) | (已弃用) 使用下一个有效观察值填充 NA/NaN 值。 |
---|---|
Series.bfill (*[, axis, inplace, limit, …]) | 使用下一个有效观察值填充 NA/NaN 值。 |
Series.dropna (*[, axis, inplace, how, …]) | 返回一个删除缺失值的新 Series。 |
Series.ffill (*[, axis, inplace, limit, …]) | 通过将最后一个有效观察结果传播到下一个有效结果来填充 NA/NaN 值。 |
Series.fillna ([value, method, axis, …]) | 使用指定方法填充 NA/NaN 值。 |
Series.interpolate ([method, axis, limit, …]) | 使用插值方法填充 NaN 值。 |
Series.isna () | 检测缺失值。 |
Series.isnull () | Series.isnull 是 Series.isna 的别名。 |
Series.notna () | 检测存在的(非缺失)值。 |
Series.notnull () | Series.notnull 是 Series.notna 的别名。 |
Series.pad (*[, axis, inplace, limit, downcast]) | (已弃用) 通过将最后一个有效观察结果传播到下一个有效结果来填充 NA/NaN 值。 |
Series.replace ([to_replace, value, inplace, …]) | 用 value 替换 to_replace 中给定的值。 |
重塑,排序
Series.argsort ([axis, kind, order, stable]) | 返回将 Series 值排序的整数索引。 |
---|---|
Series.argmin ([axis, skipna]) | 返回 Series 中最小值的整数位置。 |
Series.argmax ([axis, skipna]) | 返回 Series 中最大值的整数位置。 |
Series.reorder_levels (order) | 使用输入顺序重新排列索引级别。 |
Series.sort_values (*[, axis, ascending, …]) | 按值排序。 |
Series.sort_index (*[, axis, level, …]) | 按索引标签对 Series 进行排序。 |
Series.swaplevel ([i, j, copy]) | 在 MultiIndex 中交换级别 i 和 j。 |
Series.unstack ([level, fill_value, sort]) | 将具有 MultiIndex 的 Series 解压缩,也称为透视,以生成 DataFrame。 |
Series.explode ([ignore_index]) | 将列表样式的每个元素转换为一行。 |
Series.searchsorted (value[, side, sorter]) | 查找应插入元素以保持顺序的索引。 |
Series.ravel ([order]) | (已弃用)将扁平化的基础数据返回为 ndarray 或 ExtensionArray。 |
Series.repeat (repeats[, axis]) | 重复 Series 的元素。 |
Series.squeeze ([axis]) | 将 1 维轴对象压缩为标量。 |
Series.view ([dtype]) | (已弃用)创建 Series 的新视图。 |
合并/比较/连接/合并
Series.compare (other[, align_axis, …]) | 与另一个 Series 进行比较并显示差异。 |
---|---|
Series.update (other) | 使用传递的 Series 的值就地修改 Series。 |
与时间序列相关
Series.asfreq (freq[, method, how, …]) | 将时间序列转换为指定频率。 |
---|---|
Series.asof (where[, subset]) | 返回 where 之前没有任何 NaN 的最后一行。 |
Series.shift ([periods, freq, axis, …]) | 将索引按所需的周期数移动,可选地使用时间频率。 |
Series.first_valid_index () | 返回第一个非 NA 值的索引,如果找不到非 NA 值,则返回 None。 |
Series.last_valid_index () | 返回最后一个非 NA 值的索引,如果找不到非 NA 值,则返回 None。 |
Series.resample (rule[, axis, closed, label, …]) | 对时间序列数据重新采样。 |
Series.tz_convert (tz[, axis, level, copy]) | 将 tz-aware 轴转换为目标时区。 |
Series.tz_localize (tz[, axis, level, copy, …]) | 将序列或数据框的 tz-naive 索引本地化到目标时区。 |
Series.at_time (time[, asof, axis]) | 选择一天中特定时间的值(例如,上午 9:30)。 |
Series.between_time (start_time, end_time[, …]) | 选择一天中特定时间段的值(例如,上午 9:00-9:30)。 |
访问器
pandas 在各种访问器下提供了特定于数据类型的方法。这些是在 Series
中的单独命名空间,仅适用于特定的数据类型。
Series.str | StringMethods 的别名 |
---|---|
Series.cat | CategoricalAccessor 的别名 |
Series.dt | CombinedDatetimelikeProperties 的别名 |
Series.sparse | SparseAccessor 的别名 |
DataFrame.sparse | SparseFrameAccessor 的别名 |
Index.str | StringMethods 的别名 |
数据类型 | 访问器 |
— | — |
日期时间、时间差、周期 | dt |
字符串 | str |
分类 | cat |
稀疏 | sparse |
日期时间属性
Series.dt
可以用于访问序列的日期时间值并返回多个属性。这些可以像 Series.dt.<property>
这样访问。
日期时间属性
Series.dt.date | 返回 Python datetime.date 对象的 numpy 数组。 |
---|---|
Series.dt.time | 返回 Python datetime.time 对象的 numpy 数组。 |
Series.dt.timetz | 返回带有时区的 Python datetime.time 对象的 numpy 数组。 |
Series.dt.year | 日期时间的年份。 |
Series.dt.month | 月份,1 代表一月,12 代表十二月。 |
Series.dt.day | 日期时间的天数。 |
Series.dt.hour | 日期时间的小时数。 |
Series.dt.minute | 日期时间的分钟数。 |
Series.dt.second | 日期时间的秒数。 |
Series.dt.microsecond | 日期时间的微秒数。 |
Series.dt.nanosecond | 日期时间的纳秒数。 |
Series.dt.dayofweek | 一周中的星期几,星期一=0,星期日=6。 |
Series.dt.day_of_week | 一周中的星期几,星期一=0,星期日=6。 |
Series.dt.weekday | 一周中的星期几,星期一=0,星期日=6。 |
Series.dt.dayofyear | 年份中的第几天。 |
Series.dt.day_of_year | 年份中的第几天。 |
Series.dt.days_in_month | 月份中的天数。 |
Series.dt.quarter | 日期所在的季度。 |
Series.dt.is_month_start | 指示日期是否是月份的第一天。 |
Series.dt.is_month_end | 指示日期是否是月份的最后一天。 |
Series.dt.is_quarter_start | 指示日期是否是一个季度的第一天。 |
Series.dt.is_quarter_end | 指示日期是否是一个季度的最后一天。 |
Series.dt.is_year_start | 指示日期是否是一年的第一天。 |
Series.dt.is_year_end | 指示日期是否为年末的标志。 |
Series.dt.is_leap_year | 布尔值指示日期是否属于闰年。 |
Series.dt.daysinmonth | 月份中的天数。 |
Series.dt.days_in_month | 月份中的天数。 |
Series.dt.tz | 返回时区。 |
Series.dt.freq | 返回此 PeriodArray 的频率对象。 |
Series.dt.unit |
日期时间方法
Series.dt.isocalendar () | 根据 ISO 8601 标准计算年、周和日。 |
---|---|
Series.dt.to_period (*args, **kwargs) | 在特定频率下转换为 PeriodArray/PeriodIndex。 |
Series.dt.to_pydatetime () | (已弃用)将数据作为 datetime.datetime 对象数组返回。 |
Series.dt.tz_localize (*args, **kwargs) | 将时区无关的日期时间数组/索引本地化为时区感知的日期时间数组/索引。 |
Series.dt.tz_convert (*args, **kwargs) | 将时区感知的日期时间数组/索引从一个时区转换为另一个时区。 |
Series.dt.normalize (*args, **kwargs) | 将时间转换为午夜。 |
Series.dt.strftime (*args, **kwargs) | 使用指定的日期格式转换为索引。 |
Series.dt.round (*args, **kwargs) | 对数据执行向上取整操作到指定的频率。 |
Series.dt.floor (*args, **kwargs) | 对数据执行向下取整操作到指定的频率。 |
Series.dt.ceil (*args, **kwargs) | 对数据执行向上舍入操作以指定的频率。 |
Series.dt.month_name (*args, **kwargs) | 返回具有指定区域设置的月份名称。 |
Series.dt.day_name (*args, **kwargs) | 返回具有指定区域设置的星期几名称。 |
Series.dt.as_unit (*args, **kwargs) |
时期属性
Series.dt.qyear | |
---|---|
Series.dt.start_time | 获取周期开始的时间戳。 |
Series.dt.end_time | 获取周期结束的时间戳。 |
时间差属性
Series.dt.days | 每个元素的天数。 |
---|---|
Series.dt.seconds | 每个元素的秒数(大于等于 0 且小于 1 天)。 |
Series.dt.microseconds | 每个元素的微秒数(大于等于 0 且小于 1 秒)。 |
Series.dt.nanoseconds | 每个元素的纳秒数(大于等于 0 且小于 1 微秒)。 |
Series.dt.components | 返回时间差的各个组成部分的 DataFrame。 |
Series.dt.unit |
时间差方法
Series.dt.to_pytimedelta () | 返回一个原生的 datetime.timedelta 对象数组。 |
---|---|
Series.dt.total_seconds (*args, **kwargs) | 返回以秒为单位表示的每个元素的总持续时间。 |
| Series.dt.as_unit
(*args, **kwargs) | | ### 字符串处理
Series.str
可用于访问系列的值作为字符串,并对其应用多种方法。可以像 Series.str.<function/property>
这样访问它们。
Series.str.capitalize | 将 Series/Index 中的字符串转换为大写。 |
---|---|
Series.str.casefold | 将 Series/Index 中的字符串转换为小写。 |
Series.str.cat | 使用给定的分隔符连接 Series/Index 中的字符串。 |
Series.str.center | 在 Series/Index 中的字符串左右两侧填充。 |
Series.str.contains | 测试 Series 或 Index 中的字符串是否包含模式或正则表达式。 |
Series.str.count | 计算 Series/Index 中每个字符串中模式的出现次数。 |
Series.str.decode | 使用指定的编码对 Series/Index 中的字符串进行解码。 |
Series.str.encode | 使用指定的编码对 Series/Index 中的字符串进行编码。 |
Series.str.endswith | 测试每个字符串元素的末尾是否与模式匹配。 |
Series.str.extract | 在 DataFrame 中提取正则表达式 pat 中的捕获组作为列。 |
Series.str.extractall | 在 DataFrame 中提取正则表达式 pat 中的捕获组作为列。 |
Series.str.find | 返回 Series/Index 中每个字符串的最低索引。 |
Series.str.findall | 在 Series/Index 中查找模式或正则表达式的所有匹配项。 |
Series.str.fullmatch | 确定每个字符串是否完全匹配正则表达式。 |
Series.str.get (i) | 从指定位置或指定键提取每个组件的元素。 |
Series.str.index (sub[, start, end]) | 返回系列/索引中每个字符串中最低的索引。 |
Series.str.join (sep) | 使用指定的分隔符连接系列/索引中包含的元素列表。 |
Series.str.len () | 计算系列/索引中每个元素的长度。 |
Series.str.ljust (width[, fillchar]) | 在系列/索引中的字符串右侧填充空格。 |
Series.str.lower () | 将系列/索引中的字符串转换为小写。 |
Series.str.lstrip ([to_strip]) | 删除前导字符。 |
Series.str.match (pat[, case, flags, na]) | 确定每个字符串是否以正则表达式的匹配开头。 |
Series.str.normalize (form) | 返回系列/索引中字符串的 Unicode 规范形式。 |
Series.str.pad (width[, side, fillchar]) | 将系列/索引中的字符串填充到指定宽度。 |
Series.str.partition ([sep, expand]) | 将字符串在第一次出现的分隔符处拆分。 |
Series.str.removeprefix (prefix) | 从对象系列中删除前缀。 |
Series.str.removesuffix (suffix) | 从对象系列中删除后缀。 |
Series.str.repeat (repeats) | 在系列或索引中重复每个字符串。 |
Series.str.replace (pat, repl[, n, case, …]) | 替换系列/索引中每个出现的模式/正则表达式。 |
Series.str.rfind (sub[, start, end]) | 返回系列/索引中每个字符串中最高的索引。 |
Series.str.rindex (sub[, start, end]) | 返回系列/索引中每个字符串的最高索引。 |
Series.str.rjust (width[, fillchar]) | 在系列/索引中字符串的左侧填充。 |
Series.str.rpartition ([sep, expand]) | 在字符串中最后一次出现的位置分割字符串。 |
Series.str.rstrip ([to_strip]) | 删除字符串末尾的字符。 |
Series.str.slice ([start, stop, step]) | 从系列或索引中的每个元素中切片子字符串。 |
Series.str.slice_replace ([start, stop, repl]) | 用另一个值替换字符串的位置切片。 |
Series.str.split ([pat, n, expand, regex]) | 在给定的分隔符/定界符周围分割字符串。 |
Series.str.rsplit ([pat, n, expand]) | 在给定的分隔符/定界符周围分割字符串。 |
Series.str.startswith (pat[, na]) | 检测每个字符串元素的开头是否匹配某个模式。 |
Series.str.strip ([to_strip]) | 删除字符串的前导和尾随字符。 |
Series.str.swapcase () | 将系列/索引中的字符串进行大小写交换。 |
Series.str.title () | 将系列/索引中的字符串转换为标题格式。 |
Series.str.translate (table) | 通过给定的映射表映射字符串中的所有字符。 |
Series.str.upper () | 将系列/索引中的字符串转换为大写。 |
Series.str.wrap (width, **kwargs) | 将系列/索引中的字符串按指定行宽进行换行。 |
Series.str.zfill (width) | 在系列/索引中的字符串前面填充’0’字符。 |
Series.str.isalnum () | 检查每个字符串中的所有字符是否都是字母数字。 |
Series.str.isalpha () | 检查每个字符串中的所有字符是否都是字母。 |
Series.str.isdigit () | 检查每个字符串中的所有字符是否都是数字。 |
Series.str.isspace () | 检查每个字符串中的所有字符是否都是空格。 |
Series.str.islower () | 检查每个字符串中的所有字符是否都是小写。 |
Series.str.isupper () | 检查每个字符串中的所有字符是否都是大写。 |
Series.str.istitle () | 检查每个字符串中的所有字符是否都是首字母大写。 |
Series.str.isnumeric () | 检查每个字符串中的所有字符是否都是数字。 |
Series.str.isdecimal () | 检查每个字符串中的所有字符是否都是十进制数。 |
| Series.str.get_dummies
([sep]) | 返回 Series 的虚拟/指示变量的 DataFrame。 | ### 分类访问器
分类特定的方法和属性可以在Series.cat
访问器下使用。
Series.cat.categories | 此分类的类别。 |
---|---|
Series.cat.ordered | 类别是否具有有序关系。 |
Series.cat.codes | 返回代码的 Series 以及索引。 |
Series.cat.rename_categories (*args, **kwargs) | 重命名类别。 |
Series.cat.reorder_categories (*args, **kwargs) | 根据 new_categories 指定的顺序重新排序类别。 |
Series.cat.add_categories (*args, **kwargs) | 添加新的类别。 |
Series.cat.remove_categories (*args, **kwargs) | 移除指定的类别。 |
Series.cat.remove_unused_categories (*args, …) | 移除未使用的类别。 |
Series.cat.set_categories (*args, **kwargs) | 将类别设置为指定的新类别。 |
Series.cat.as_ordered (*args, **kwargs) | 将分类设置为有序。 |
| Series.cat.as_unordered
(*args, **kwargs) | 将分类设置为无序。 | ### 稀疏访问器
稀��-dtype 特定的方法和属性可在 Series.sparse
访问器下找到。
Series.sparse.npoints | 非 fill_value 点的数量。 |
---|---|
Series.sparse.density | 非 fill_value 点的百分比,以小数表示。 |
Series.sparse.fill_value | 数据中的 fill_value 元素不会被存储。 |
Series.sparse.sp_values | 包含非 fill_value 值的 ndarray。 |
Series.sparse.from_coo (A[, dense_index]) | 从 scipy.sparse.coo_matrix 创建具有稀疏值的 Series。 |
| Series.sparse.to_coo
([row_levels, …]) | 从具有 MultiIndex 的 Series 创建一个 scipy.sparse.coo_matrix。 | ### 列表访问器
列表-dtype 特定的方法和属性可在 Series.list
访问器下找到。
Series.list.flatten () | 展平列表值。 |
---|---|
Series.list.len () | 返回 Series 中每个列表的长度。 |
| Series.list.__getitem__
(key) | 在 Series 中索引或切片列表。 | ### 结构访问器
箭头结构体 dtype 特定方法和属性在Series.struct
访问器下提供。
Series.struct.dtypes | 返回结构体的每个子字段的 dtype 对象。 |
---|---|
Series.struct.field (name_or_index) | 将结构体的子字段提取为 Series。 |
| Series.struct.explode
() | 将结构体的所有子字段提取为 DataFrame。 | ### 标志
标志指的是 pandas 对象的属性。数据集的属性(如记录日期、访问的 URL 等)应存储在Series.attrs
中。
| Flags
(obj, *, allows_duplicate_labels) | 适用于 pandas 对象的标志。 | ### 元数据
Series.attrs
是用于存储此 Series 的全局元数据的字典。
警告
Series.attrs
被视为实验性内容,可能会在没有警告的情况下更改。
Series.attrs | 此数据集的全局属性字典。 |
---|
绘图
Series.plot
既是一个可调用方法,也是特定绘图方法的命名空间属性,形式为Series.plot.<kind>
。
Series.plot ([kind, ax, figsize, …]) | Series 绘图访问器和方法 |
---|---|
Series.plot.area ([x, y, stacked]) | 绘制堆叠面积图。 |
Series.plot.bar ([x, y]) | 垂直条形图。 |
Series.plot.barh ([x, y]) | 绘制水平条形图。 |
Series.plot.box ([by]) | 绘制 DataFrame 列的箱线图。 |
Series.plot.density ([bw_method, ind]) | 使用高斯核生成核密度估计图。 |
Series.plot.hist ([by, bins]) | 绘制 DataFrame 列的直方图。 |
Series.plot.kde ([bw_method, ind]) | 使用高斯核生成核密度估计图。 |
Series.plot.line ([x, y]) | 将 Series 或 DataFrame 绘制为折线图。 |
Series.plot.pie (**kwargs) | 生成饼图。 |
Series.hist ([by, ax, grid, xlabelsize, …]) | 使用 matplotlib 绘制输入 Series 的直方图。 |
序列化 / IO / 转换
Series.to_pickle (path, *[, compression, …]) | 将对象序列化为文件。 |
---|---|
Series.to_csv ([path_or_buf, sep, na_rep, …]) | 将对象写入逗号分隔值(csv)文件。 |
Series.to_dict (*[, into]) | 将 Series 转换为 {标签 -> 值} 字典或类似字典的对象。 |
Series.to_excel (excel_writer, *[, …]) | 将对象写入 Excel 表格。 |
Series.to_frame ([name]) | 将 Series 转换为 DataFrame。 |
Series.to_xarray () | 从 pandas 对象返回一个 xarray 对象。 |
Series.to_hdf (path_or_buf, *, key[, mode, …]) | 使用 HDFStore 将包含的数据写入 HDF5 文件。 |
Series.to_sql (name, con, *[, schema, …]) | 将存储在 DataFrame 中的记录写入 SQL 数据库。 |
Series.to_json ([path_or_buf, orient, …]) | 将对象转换为 JSON 字符串。 |
Series.to_string ([buf, na_rep, …]) | 呈现 Series 的字符串表示。 |
Series.to_clipboard (*[, excel, sep]) | 将对象复制到系统剪贴板。 |
Series.to_latex ([buf, columns, header, …]) | 将对象呈现为 LaTeX 表格、长表格或嵌套表格。 |
Series.to_markdown ([buf, mode, index, …]) | 以 Markdown 友好的格式打印 Series。 |
构造函数
Series ([data, index, dtype, name, copy, …]) | 具有轴标签的一维 ndarray(包括时间序列)。 |
---|
属性
轴
Series.index | Series 的索引(轴标签)。 |
---|---|
Series.array | 支持此 Series 或 Index 的数据的 ExtensionArray。 |
Series.values | 根据 dtype 返回 Series 作为 ndarray 或类似 ndarray。 |
Series.dtype | 返回底层数据的 dtype 对象。 |
Series.shape | 返回底层数据的形状的元组。 |
Series.nbytes | 返回底层数据的字节数。 |
Series.ndim | 底层数据的维数,根据定义为 1。 |
Series.size | 返回底层数据中的元素数。 |
Series.T | 返回转置,根据定义是自身。 |
Series.memory_usage ([index, deep]) | 返回 Series 的内存使用情况。 |
Series.hasnans | 如果存在任何 NaN,则返回 True。 |
Series.empty | 指示 Series/DataFrame 是否为空。 |
Series.dtypes | 返回底层数据的 dtype 对象。 |
Series.name | 返回 Series 的名称。 |
Series.flags | 获取与此 pandas 对象关联的属性。 |
Series.set_flags (*[, copy, …]) | 返回具有更新标志的新对象。 |
转换
Series.astype (dtype[, copy, errors]) | 将 pandas 对象转换为指定的 dtype dtype 。 |
---|---|
Series.convert_dtypes ([infer_objects, …]) | 使用支持 pd.NA 的 dtype 将列转换为可能的最佳 dtype。 |
Series.infer_objects ([copy]) | 尝试推断对象列的更好数据类型。 |
Series.copy ([deep]) | 复制此对象的索引和数据。 |
Series.bool () | (已弃用)返回单个元素 Series 或 DataFrame 的布尔值。 |
Series.to_numpy ([dtype, copy, na_value]) | 表示此 Series 或 Index 中的值的 NumPy ndarray。 |
Series.to_period ([freq, copy]) | 将 Series 从 DatetimeIndex 转换为 PeriodIndex。 |
Series.to_timestamp ([freq, how, copy]) | 转换为时间戳的 DatetimeIndex,位于周期的开始。 |
Series.to_list () | 返回值的列表。 |
Series.__array__ ([dtype, copy]) | 将值作为 NumPy 数组返回。 |
索引,迭代
Series.get (key[, default]) | 获取给定键(例如:DataFrame 列)的对象中的项目。 |
---|---|
Series.at | 访问行/列标签对的单个值。 |
Series.iat | 通过整数位置访问行/列对的单个值。 |
Series.loc | 通过标签或布尔数组访问一组行和列。 |
Series.iloc | (已弃用)基于纯整数位置的按位置选择索引。 |
Series.__iter__ () | 返回值的迭代器。 |
Series.items () | 惰性地遍历(索引,值)元组。 |
Series.keys () | 返回索引的别名。 |
Series.pop (item) | 返回项目并从系列中删除。 |
Series.item () | 将底层数据的第一个元素作为 Python 标量返回。 |
Series.xs (key[, axis, level, drop_level]) | 从系列/数据框中返回横截面。 |
更多关于 .at
, .iat
, .loc
, 和 .iloc
的信息,请参阅索引文档。
二元操作符函数
Series.add (other[, level, fill_value, axis]) | 返回系列和其他的加法,逐元素进行计算(二元操作符 add)。 |
---|---|
Series.sub (other[, level, fill_value, axis]) | 返回系列和其他的减法,逐元素进行计算(二元操作符 sub)。 |
Series.mul (other[, level, fill_value, axis]) | 返回系列和其他的乘法,逐元素进行计算(二元操作符 mul)。 |
Series.div (other[, level, fill_value, axis]) | 返回系列和其他的浮点除法,逐元素进行计算(二元操作符 truediv)。 |
Series.truediv (other[, level, fill_value, axis]) | 返回系列和其他的浮点除法,逐元素进行计算(二元操作符 truediv)。 |
Series.floordiv (other[, level, fill_value, axis]) | 返回系列和其他的整除,逐元素进行计算(二元操作符 floordiv)。 |
Series.mod (other[, level, fill_value, axis]) | 返回系列和其他的模数,逐元素进行计算(二元操作符 mod)。 |
Series.pow (other[, level, fill_value, axis]) | 返回系列和其他的指数幂,逐元素进行计算(二元操作符 pow)。 |
Series.radd (other[, level, fill_value, axis]) | 返回系列和其他的加法,逐元素进行计算(二元操作符 radd)。 |
Series.rsub (other[, level, fill_value, axis]) | 返回系列和其他的减法,逐元素进行计算(二元操作符 rsub)。 |
Series.rmul (other[, level, fill_value, axis]) | 返回系列和其他的乘法,逐元素进行计算(二元操作符 rmul)。 |
Series.rdiv (other[, level, fill_value, axis]) | 返回系列和其他的浮点除法,逐元素进行计算(二元操作符 rtruediv)。 |
Series.rtruediv (other[, level, fill_value, axis]) | 返回 Series 和其他的浮点数除法,逐元素进行计算(二元运算符 rtruediv)。 |
Series.rfloordiv (other[, level, fill_value, …]) | 返回 Series 和其他的整数除法,逐元素进行计算(二元运算符 rfloordiv)。 |
Series.rmod (other[, level, fill_value, axis]) | 返回 Series 和其他的模数,逐元素进行计算(二元运算符 rmod)。 |
Series.rpow (other[, level, fill_value, axis]) | 返回 Series 和其他的指数幂,逐元素进行计算(二元运算符 rpow)。 |
Series.combine (other, func[, fill_value]) | 根据 func 将 Series 与 Series 或标量组合在一起。 |
Series.combine_first (other) | 使用 ‘other’ 中相同位置的值更新空元素。 |
Series.round ([decimals]) | 将 Series 中的每个值四舍五入到指定的小数位数。 |
Series.lt (other[, level, fill_value, axis]) | 返回 Series 和其他的小于值,逐元素进行比较(二元运算符 lt)。 |
Series.gt (other[, level, fill_value, axis]) | 返回 Series 和其他的大于值,逐元素进行比较(二元运算符 gt)。 |
Series.le (other[, level, fill_value, axis]) | 返回 Series 和其他的小于或等于值,逐元素进行比较(二元运算符 le)。 |
Series.ge (other[, level, fill_value, axis]) | 返回 Series 和其他的大于或等于值,逐元素进行比较(二元运算符 ge)。 |
Series.ne (other[, level, fill_value, axis]) | 返回 Series 和其他的不相等值,逐元素进行比较(二元运算符 ne)。 |
Series.eq (other[, level, fill_value, axis]) | 返回 Series 和其他的相等值,逐元素进行比较(二元运算符 eq)。 |
Series.product ([axis, skipna, numeric_only, …]) | 返回请求轴上值的乘积。 |
Series.dot (other) | 计算 Series 和其他列之间的点积。 |
函数应用、GroupBy 和窗口
Series.apply (func[, convert_dtype, args, by_row]) | 对 Series 的值调用函数。 |
---|---|
Series.agg ([func, axis]) | 在指定轴上使用一个或多个操作进行聚合。 |
Series.aggregate ([func, axis]) | 在指定轴上使用一个或多个操作进行聚合。 |
Series.transform (func[, axis]) | 在自身上调用 func ,生成一个与自身轴形状相同的 Series。 |
Series.map (arg[, na_action]) | 根据输入映射或函数对 Series 的值进行映射。 |
Series.groupby ([by, axis, level, as_index, …]) | 使用映射器或一系列列对 Series 进行分组。 |
Series.rolling (window[, min_periods, …]) | 提供滚动窗口计算。 |
Series.expanding ([min_periods, axis, method]) | 提供扩展窗口计算。 |
Series.ewm ([com, span, halflife, alpha, …]) | 提供指数加权(EW)计算。 |
Series.pipe (func, *args, **kwargs) | 应用可链式调用的函数,期望 Series 或 DataFrames。 |
计算 / 描述性统计
Series.abs () | 返回每个元素的绝对数值的 Series/DataFrame。 |
---|---|
Series.all ([axis, bool_only, skipna]) | 返回所有元素是否都为 True,可能在一个轴上。 |
Series.any (*[, axis, bool_only, skipna]) | 返回任何元素��否为 True,可能在一个轴上。 |
Series.autocorr ([lag]) | 计算滞后 N 的自相关性。 |
Series.between (left, right[, inclusive]) | 返回布尔 Series,相当于 left <= series <= right。 |
Series.clip ([lower, upper, axis, inplace]) | 在输入阈值处修剪值。 |
Series.corr (other[, method, min_periods]) | 计算与其他 Series 的相关性,排除缺失值。 |
Series.count () | 返回 Series 中非 NA/null 观测值的数量。 |
Series.cov (other[, min_periods, ddof]) | 计算与 Series 的协方差,排除缺失值。 |
Series.cummax ([axis, skipna]) | 返回 DataFrame 或 Series 轴上的累积最大值。 |
Series.cummin ([axis, skipna]) | 返回 DataFrame 或 Series 轴上的累积最小值。 |
Series.cumprod ([axis, skipna]) | 返回 DataFrame 或 Series 轴上的累积乘积。 |
Series.cumsum ([axis, skipna]) | 返回 DataFrame 或 Series 轴上的累积总和。 |
Series.describe ([percentiles, include, exclude]) | 生成描述性统计信息。 |
Series.diff ([periods]) | 元素的第一个离散差异。 |
Series.factorize ([sort, use_na_sentinel]) | 将对象编码为枚举类型或分类变量。 |
Series.kurt ([axis, skipna, numeric_only]) | 返回请求轴上的无偏峰度。 |
Series.max ([axis, skipna, numeric_only]) | 返回请求轴上的最大值。 |
Series.mean ([axis, skipna, numeric_only]) | 返回请求轴上的均值。 |
Series.median ([axis, skipna, numeric_only]) | 返回请求轴上的中位数。 |
Series.min ([axis, skipna, numeric_only]) | 返回请求轴上的最小值。 |
Series.mode ([dropna]) | 返回 Series 的众数。 |
Series.nlargest ([n, keep]) | 返回最大的 n 个元素。 |
Series.nsmallest ([n, keep]) | 返回最小的 n 个元素。 |
Series.pct_change ([periods, fill_method, …]) | 当前元素与先前元素之间的分数变化。 |
Series.prod ([axis, skipna, numeric_only, …]) | 返回请求轴上值的乘积。 |
Series.quantile ([q, interpolation]) | 返回给定分位数处的值。 |
Series.rank ([axis, method, numeric_only, …]) | 沿轴计算数据的排名(1 到 n)。 |
Series.sem ([axis, skipna, ddof, numeric_only]) | 返回请求轴上均值的无偏标准误差。 |
Series.skew ([axis, skipna, numeric_only]) | 返回请求轴上的无偏偏度。 |
Series.std ([axis, skipna, ddof, numeric_only]) | 返回请求轴上的样��标准差。 |
Series.sum ([axis, skipna, numeric_only, …]) | 返回请求轴上值的总和。 |
Series.var ([axis, skipna, ddof, numeric_only]) | 返回请求轴上的无偏方差。 |
Series.kurtosis ([axis, skipna, numeric_only]) | 返回请求轴上的无偏峰度。 |
Series.unique () | 返回 Series 对象的唯一值。 |
Series.nunique ([dropna]) | 返回对象中唯一元素的数量。 |
Series.is_unique | 如果对象中的值是唯一的,则返回布尔值。 |
Series.is_monotonic_increasing | 如果对象中的值单调递增,则返回布尔值。 |
Series.is_monotonic_decreasing | 如果对象中的值单调递减,则返回布尔值。 |
Series.value_counts ([normalize, sort, …]) | 返回包含唯一值计数的 Series。 |
重新索引/选择/标签操作
Series.align (other[, join, axis, level, …]) | 使用指定的连接方法在它们的轴上对齐两个对象。 |
---|---|
Series.case_when (caselist) | 替换条件为 True 的值。 |
Series.drop ([labels, axis, index, columns, …]) | 返回删除指定索引标签的 Series。 |
Series.droplevel (level[, axis]) | 返回请求删除的索引/列级别的 Series/DataFrame。 |
Series.drop_duplicates (*[, keep, inplace, …]) | 返回删除重复值的 Series。 |
Series.duplicated ([keep]) | 指示重复的 Series 值。 |
Series.equals (other) | 测试两个对象是否包含相同的元素。 |
Series.first (offset) | (已弃用) 根据日期偏移量选择时间序列数据的初始周期。 |
Series.head ([n]) | 返回前 n 行。 |
Series.idxmax ([axis, skipna]) | 返回最大值的行标签。 |
Series.idxmin ([axis, skipna]) | 返回最小值的行标签。 |
Series.isin (values) | Series 中的元素是否包含在 values 中。 |
Series.last (offset) | (已弃用) 根据日期偏移量选择时间序列数据的最终周期。 |
Series.reindex ([index, axis, method, copy, …]) | 将 Series 调整为具有新索引的对象,并可选择填充逻辑。 |
Series.reindex_like (other[, method, copy, …]) | 返回具有与其他对象匹配索引的对象。 |
Series.rename ([index, axis, copy, inplace, …]) | 更改 Series 索引标签或名称。 |
Series.rename_axis ([mapper, index, axis, …]) | 设置索引或列的轴名称。 |
Series.reset_index ([level, drop, name, …]) | 生成重置索引的新 DataFrame 或 Series。 |
Series.sample ([n, frac, replace, weights, …]) | 从对象的轴中返回随机样本项。 |
Series.set_axis (labels, *[, axis, copy]) | 为给定轴分配所需的索引。 |
Series.take (indices[, axis]) | 返回沿轴的给定位置索引的元素。 |
Series.tail ([n]) | 返回最后 n 行。 |
Series.truncate ([before, after, axis, copy]) | 在某个索引值之前和之后截断 Series 或 DataFrame。 |
Series.where (cond[, other, inplace, axis, level]) | 替换条件为 False 的值。 |
Series.mask (cond[, other, inplace, axis, level]) | 替换条件为 True 的值。 |
Series.add_prefix (prefix[, axis]) | 使用字符串前缀为标签添加前缀。 |
Series.add_suffix (suffix[, axis]) | 使用字符串后缀为标签添加后缀。 |
Series.filter ([items, like, regex, axis]) | 根据指定的索引标签筛选数据帧的行或列。 |
缺失数据处理
Series.backfill (*[, axis, inplace, limit, …]) | (已弃用) 使用下一个有效观察值填充 NA/NaN 值。 |
---|---|
Series.bfill (*[, axis, inplace, limit, …]) | 使用下一个有效观察值填充 NA/NaN 值。 |
Series.dropna (*[, axis, inplace, how, …]) | 返回删除缺失值的新 Series。 |
Series.ffill (*[, axis, inplace, limit, …]) | 通过将最后一个有效观测值传播到下一个有效值来填充 NA/NaN 值。 |
Series.fillna ([value, method, axis, …]) | 使用指定方法填充 NA/NaN 值。 |
Series.interpolate ([method, axis, limit, …]) | 使用插值方法填充 NaN 值。 |
Series.isna () | 检测缺失值。 |
Series.isnull () | Series.isnull 是 Series.isna 的别名。 |
Series.notna () | 检测存在的(非缺失)值。 |
Series.notnull () | Series.notnull 是 Series.notna 的别名。 |
Series.pad (*[, axis, inplace, limit, downcast]) | (已弃用)通过将最后一个有效观测值传播到下一个有效值来填充 NA/NaN 值。 |
Series.replace ([to_replace, value, inplace, …]) | 用 value 替换 to_replace 中的值。 |
重塑、排序
Series.argsort ([axis, kind, order, stable]) | 返回对 Series 值进行排序的整数索引。 |
---|---|
Series.argmin ([axis, skipna]) | 返回 Series 中最小值的整数位置。 |
Series.argmax ([axis, skipna]) | 返回 Series 中最大值的整数位置。 |
Series.reorder_levels (order) | 使用输入顺序重新排列索引级别。 |
Series.sort_values (*[, axis, ascending, …]) | 按值排序。 |
Series.sort_index (*[, axis, level, …]) | 按索引标签对 Series 进行排序。 |
Series.swaplevel ([i, j, copy]) | 在MultiIndex 中交换级别 i 和 j。 |
Series.unstack ([level, fill_value, sort]) | Unstack,也称为 pivot,将具有 MultiIndex 的 Series 转换为 DataFrame。 |
Series.explode ([ignore_index]) | 将类似列表的每个元素转换为一行。 |
Series.searchsorted (value[, side, sorter]) | 查找应插入元素以保持顺序的索引。 |
Series.ravel ([order]) | (已弃用) 将底层数据扁平化为 ndarray 或 ExtensionArray。 |
Series.repeat (repeats[, axis]) | 重复 Series 的元素。 |
Series.squeeze ([axis]) | 将 1 维轴对象压缩为标量。 |
Series.view ([dtype]) | (已弃用) 创建 Series 的新视图。 |
合并/比较/连接/合并
Series.compare (other[, align_axis, …]) | 与另一个 Series 进行比较并显示差异。 |
---|---|
Series.update (other) | 使用传递的 Series 的值就地修改 Series。 |
与时间序列相关
Series.asfreq (freq[, method, how, …]) | 将时间序列转换为指定的频率。 |
---|---|
Series.asof (where[, subset]) | 返回在 where 之前没有任何 NaN 的最后一行。 |
Series.shift ([periods, freq, axis, …]) | 通过所需的周期数将索引向前或向后移动,可选择性地使用时间频率。 |
Series.first_valid_index () | 返回第一个非 NA 值的索引,如果找不到非 NA 值,则返回 None。 |
Series.last_valid_index () | 返回最后一个非 NA 值的索引,如果找不到非 NA 值,则返回 None。 |
Series.resample (rule[, axis, closed, label, …]) | 重新采样时间序列数据。 |
Series.tz_convert (tz[, axis, level, copy]) | 将带有时区信息的轴转换为目标时区。 |
Series.tz_localize (tz[, axis, level, copy, …]) | 将 Series 或 DataFrame 的 tz-naive 索引本地化到目标时区。 |
Series.at_time (time[, asof, axis]) | 选择一天中特定时间的值(例如,上午 9:30)。 |
Series.between_time (start_time, end_time[, …]) | 选择一天中特定时间段内的值(例如,上午 9:00-9:30)。 |
访问器
pandas 在各种访问器下提供了特定于数据类型的方法。这些是 Series
内的单独命名空间,仅适用于特定数据类型。
Series.str | StringMethods 的别名 |
---|---|
Series.cat | CategoricalAccessor 的别名 |
Series.dt | CombinedDatetimelikeProperties 的别名 |
Series.sparse | SparseAccessor 的别名 |
DataFrame.sparse | SparseFrameAccessor 的别名 |
Index.str | StringMethods 的别名 |
数据类型 | 访问器 |
— | — |
日期时间、时间差、周期 | dt |
字符串 | str |
Categorical | cat |
稀疏 | sparse |
日期时间属性
Series.dt
可用于访问系列的日期时间属性并返回多个属性。这些属性可以像 Series.dt.<property>
这样访问。
日期时间属性
Series.dt.date | 返回带有 python datetime.date 对象的 numpy 数组。 |
---|---|
Series.dt.time | 返回带有时区的 datetime.time 对象的 numpy 数组。 |
Series.dt.timetz | 返回带有时区的 datetime.time 对象的 numpy 数组。 |
Series.dt.year | 日期时间的年份。 |
Series.dt.month | 月份,1 代表一月,12 代表十二月。 |
Series.dt.day | 日期时间的日数。 |
Series.dt.hour | 日期时间的小时数。 |
Series.dt.minute | 日期时间的分钟数。 |
Series.dt.second | 日期时间的秒数。 |
Series.dt.microsecond | 日期时间的微秒数。 |
Series.dt.nanosecond | 日期时间的纳秒数。 |
Series.dt.dayofweek | 星期几,星期一=0,星期日=6。 |
Series.dt.day_of_week | 星期几,星期一=0,星期日=6。 |
Series.dt.weekday | 星期几,星期一=0,星期日=6。 |
Series.dt.dayofyear | 年份中的第几天。 |
Series.dt.day_of_year | 年份中的第几天。 |
Series.dt.days_in_month | 该月的天数。 |
Series.dt.quarter | 日期的季度。 |
Series.dt.is_month_start | 是否为月初的指示器。 |
Series.dt.is_month_end | 是否为月末的指示器。 |
Series.dt.is_quarter_start | 是否为季度第一天的指示器。 |
Series.dt.is_quarter_end | 是否为季度最后一天的指示器。 |
Series.dt.is_year_start | 是否为年初的指示器。 |
Series.dt.is_year_end | 表示日期是否为年末的指示器。 |
Series.dt.is_leap_year | 如果日期属于闰年,则为布尔值指示器。 |
Series.dt.daysinmonth | 一个月中的天数。 |
Series.dt.days_in_month | 一个月中的天数。 |
Series.dt.tz | 返回时区。 |
Series.dt.freq | 返回此 PeriodArray 的频率对象。 |
Series.dt.unit |
时间方法
Series.dt.isocalendar () | 根据 ISO 8601 标准计算年份、周数和日期。 |
---|---|
Series.dt.to_period (*args, **kwargs) | 在特定频率下转换为 PeriodArray/PeriodIndex。 |
Series.dt.to_pydatetime () | (已弃用)将数据返回为 datetime.datetime 对象数组。 |
Series.dt.tz_localize (*args, **kwargs) | 将时区无关的日期时间数组/索引本地化为时区感知的日期时间数组/索引。 |
Series.dt.tz_convert (*args, **kwargs) | 将时区感知的日期时间数组/索引从一个时区转换为另一个时区。 |
Series.dt.normalize (*args, **kwargs) | 将时间转换为午夜。 |
Series.dt.strftime (*args, **kwargs) | 使用指定的日期格式转换为索引。 |
Series.dt.round (*args, **kwargs) | 对数据执行舍入操作,以指定的频率。 |
Series.dt.floor (*args, **kwargs) | 对数据执行向下取整操作,以指定的频率。 |
Series.dt.ceil (*args, **kwargs) | 对数据执行向上取整操作到指定的频率。 |
Series.dt.month_name (*args, **kwargs) | 返回指定语言环境的月份名称。 |
Series.dt.day_name (*args, **kwargs) | 返回指定语言环境的星期几名称。 |
Series.dt.as_unit (*args, **kwargs) |
期间属性
Series.dt.qyear | |
---|---|
Series.dt.start_time | 获取周期开始的时间戳。 |
Series.dt.end_time | 获取周期结束的时间戳。 |
时间增量属性
Series.dt.days | 每个元素的天数。 |
---|---|
Series.dt.seconds | 每个元素的秒数(大于等于 0 且小于 1 天)。 |
Series.dt.microseconds | 每个元素的微秒数(大于等于 0 且小于 1 秒)。 |
Series.dt.nanoseconds | 每个元素的纳秒数(大于等于 0 且小于 1 微秒)。 |
Series.dt.components | 返回时间增量的各个组成部分的数据框。 |
Series.dt.unit |
时间增量方法
Series.dt.to_pytimedelta () | 返回一个原生datetime.timedelta 对象数组。 |
---|---|
Series.dt.total_seconds (*args, **kwargs) | 返回每个元素表示的总持续时间(以秒为单位)。 |
| Series.dt.as_unit
(*args, **kwargs) | | ### 字符串处理
Series.str
可用于访问系列的值作为字符串并对其应用多种方法。这些可以像 Series.str.<function/property>
这样访问。
Series.str.capitalize () | 将 Series/Index 中的字符串转换为大写。 |
---|---|
Series.str.casefold () | 将 Series/Index 中的字符串转换为 casefolded。 |
Series.str.cat ([others, sep, na_rep, join]) | 使用给定的分隔符连接 Series/Index 中的字符串。 |
Series.str.center (width[, fillchar]) | 在 Series/Index 中的字符串的左右两侧填充。 |
Series.str.contains (pat[, case, flags, na, …]) | 测试 Series 或 Index 中的字符串是否包含模式或正则表达式。 |
Series.str.count (pat[, flags]) | 计算 Series/Index 中每个字符串中模式的出现次数。 |
Series.str.decode (encoding[, errors]) | 使用指定的编码解码 Series/Index 中的字符字符串。 |
Series.str.encode (encoding[, errors]) | 使用指定的编码对 Series/Index 中的字符字符串进行编码。 |
Series.str.endswith (pat[, na]) | 测试每个字符串元素的结尾是否与模式匹配。 |
Series.str.extract (pat[, flags, expand]) | 在 DataFrame 中提取正则表达式 pat 中的捕获组作为列。 |
Series.str.extractall (pat[, flags]) | 在 DataFrame 中提取正则表达式 pat 中的捕获组作为列。 |
Series.str.find (sub[, start, end]) | 返回 Series/Index 中每个字符串中的最低索引。 |
Series.str.findall (pat[, flags]) | 在 Series/Index 中查找模式或正则表达式的所有匹配项。 |
Series.str.fullmatch (pat[, case, flags, na]) | 确定每个字符串是否完全匹配正则表达式。 |
Series.str.get (i) | 从指定位置或具有指定键的每个组件中提取元素。 |
Series.str.index (sub[, start, end]) | 返回 Series/Index 中每个字符串中的最低索引。 |
Series.str.join (sep) | 使用传递的分隔符连接 Series/Index 中作为元素包含的列表。 |
Series.str.len () | 计算 Series/Index 中每个元素的长度。 |
Series.str.ljust (width[, fillchar]) | 在 Series/Index 中字符串的右侧填充。 |
Series.str.lower () | 将 Series/Index 中的字符串转换为小写。 |
Series.str.lstrip ([to_strip]) | 移除前导字符。 |
Series.str.match (pat[, case, flags, na]) | 确定每个字符串是否以正则表达式的匹配开头。 |
Series.str.normalize (form) | 返回 Series/Index 中字符串的 Unicode 标准形式。 |
Series.str.pad (width[, side, fillchar]) | 将 Series/Index 中的字符串填充到指定宽度。 |
Series.str.partition ([sep, expand]) | 在第一个分隔符处拆分字符串。 |
Series.str.removeprefix (prefix) | 从对象系列中移除前缀。 |
Series.str.removesuffix (suffix) | 从对象系列中移���后缀。 |
Series.str.repeat (repeats) | 在 Series 或 Index 中复制每个字符串。 |
Series.str.replace (pat, repl[, n, case, …]) | 替换 Series/Index 中每个出现的模式/正则表达式。 |
Series.str.rfind (sub[, start, end]) | 返回 Series/Index 中每个字符串中的最高索引。 |
Series.str.rindex (sub[, start, end]) | 返回 Series/Index 中每个字符串中的最高索引。 |
Series.str.rjust (width[, fillchar]) | 在 Series/Index 中的字符串左侧填充。 |
Series.str.rpartition ([sep, expand]) | 在字符串中最后一次出现的位置分割字符串。 |
Series.str.rstrip ([to_strip]) | 移除字符串末尾的字符。 |
Series.str.slice ([start, stop, step]) | 从 Series 或 Index 中的每个元素中切片子字符串。 |
Series.str.slice_replace ([start, stop, repl]) | 用另一个值替换字符串的位置切片。 |
Series.str.split ([pat, n, expand, regex]) | 在给定的分隔符周围分割字符串。 |
Series.str.rsplit ([pat, n, expand]) | 在给定的分隔符周围分割字符串。 |
Series.str.startswith (pat[, na]) | 测试每个字符串元素的开头是否与模式匹配。 |
Series.str.strip ([to_strip]) | 移除字符串的前导和尾随字符。 |
Series.str.swapcase () | 将 Series/Index 中的字符串转换为大小写互换。 |
Series.str.title () | 将 Series/Index 中的字符串转换为首字母大写。 |
Series.str.translate (table) | 将字符串中的所有字符通过给定的映射表进行映射。 |
Series.str.upper () | 将 Series/Index 中的字符串转换为大写。 |
Series.str.wrap (width, **kwargs) | 在指定的行宽处将 Series/Index 中的字符串换行。 |
Series.str.zfill (width) | 在 Series/Index 中的字符串前面填充 ‘0’ 字符。 |
Series.str.isalnum () | 检查每个字符串中的所有字符是否为字母数字字符。 |
Series.str.isalpha () | 检查每个字符串中的所有字符是否为字母字符。 |
Series.str.isdigit () | 检查每个字符串中的所有字符是否为数字。 |
Series.str.isspace () | 检查每个字符串中的所有字符是否为空格字符。 |
Series.str.islower () | 检查每个字符串中的所有字符是否为小写。 |
Series.str.isupper () | 检查每个字符串中的所有字符是否为大写。 |
Series.str.istitle () | 检查每个字符串中的所有字符是否为标题大小写。 |
Series.str.isnumeric () | 检查每个字符串中的所有字符是否为数字字符。 |
Series.str.isdecimal () | 检查每个字符串中的所有字符是否为十进制数。 |
| Series.str.get_dummies
([sep]) | 返回 Series 的虚拟/指示变量的 DataFrame。 | ### 分类访问器
分类数据类型特定的方法和属性可在Series.cat
访问器下使用。
Series.cat.categories | 此分类的类别。 |
---|---|
Series.cat.ordered | 类别是否具有有序关系。 |
Series.cat.codes | 返回代码系列以及索引。 |
Series.cat.rename_categories (*args, **kwargs) | 重命名类别。 |
Series.cat.reorder_categories (*args, **kwargs) | 按照新类别指定的顺序重新排序类别。 |
Series.cat.add_categories (*args, **kwargs) | 添加新的分类。 |
Series.cat.remove_categories (*args, **kwargs) | 移除指定的分类。 |
Series.cat.remove_unused_categories (*args, …) | 移除未使用的分类。 |
Series.cat.set_categories (*args, **kwargs) | 将分类设置为指定的新分类。 |
Series.cat.as_ordered (*args, **kwargs) | 将分类设置为有序。 |
| Series.cat.as_unordered
(*args, **kwargs) | 将分类设置为无序。 ### 稀疏访问器
稀疏数据类型特定的方法和属性可在 Series.sparse
访问器下找到。
Series.sparse.npoints | 非 fill_value 点的数量。 |
---|---|
Series.sparse.density | 非 fill_value 点的百分比,以小数表示。 |
Series.sparse.fill_value | 数据中的 fill_value 元素不会被存储。 |
Series.sparse.sp_values | 包含非 fill_value 值的 ndarray。 |
Series.sparse.from_coo (A[, dense_index]) | 从 scipy.sparse.coo_matrix 创建具有稀疏值的 Series。 |
| Series.sparse.to_coo
([row_levels, …]) | 从具有 MultiIndex 的 Series 创建 scipy.sparse.coo_matrix。 ### 列表访问器
箭头列表数据类型特定的方法和属性可在 Series.list
访问器下找到。
Series.list.flatten () | 展平列表值。 |
---|---|
Series.list.len () | 返回 Series 中每个列表的长度。 |
| Series.list.__getitem__
(key) | 在 Series 中索引或切片列表。 | ### 结构访问器
Arrow 结构 dtype 特定的方法和属性在 Series.struct
访问器下提供。
Series.struct.dtypes | 返回结构的每个子字段的 dtype 对象。 |
---|---|
Series.struct.field (name_or_index) | 将结构的子字段提取为 Series。 |
| Series.struct.explode
() | 将结构的所有子字段提取为 DataFrame。 | ### 标志
标志指的是 pandas 对象的属性。数据集的属性(如记录日期、访问的 URL 等)应存储在 Series.attrs
中。
| Flags
(obj, *, allows_duplicate_labels) | 适用于 pandas 对象的标志。 | ### 元数据
Series.attrs
是一个用于存储此 Series 的全局元数据的字典。
警告
Series.attrs
被视为实验性的,可能会在没有警告的情况下更改。
| Series.attrs
| 此数据集的全局属性字典。 | ### 日期时间属性
Series.dt
可用于访问系列的日期时间值并返回多个属性。这些可以像 Series.dt.<property>
这样访问。
日期时间属性
Series.dt.date | 返回 python 的 numpy 数组datetime.date 对象。 |
---|---|
Series.dt.time | 返回带有时区的datetime.time 对象的 numpy 数组。 |
Series.dt.timetz | 返回带有时区的datetime.time 对象的 numpy 数组。 |
Series.dt.year | 日期时间的年份。 |
Series.dt.month | 月份,一月为 1,十二月为 12。 |
Series.dt.day | 日期时间的日期。 |
Series.dt.hour | 日期时间的小时。 |
Series.dt.minute | 日期时间的分钟。 |
Series.dt.second | 日期时间的秒。 |
Series.dt.microsecond | 日期时间的微秒。 |
Series.dt.nanosecond | 日期时间的纳秒。 |
Series.dt.dayofweek | 一周中的日期,星期一=0,星期日=6。 |
Series.dt.day_of_week | 一周中的日期,星期一=0,星期日=6。 |
Series.dt.weekday | 一周中的日期,星期一=0,星期日=6。 |
Series.dt.dayofyear | 一年中的日期序数。 |
Series.dt.day_of_year | 一年中的日期序数。 |
Series.dt.days_in_month | 月份的天数。 |
Series.dt.quarter | 日期的季度。 |
Series.dt.is_month_start | 表示日期是否为月初。 |
Series.dt.is_month_end | 表示日期是否为月末。 |
Series.dt.is_quarter_start | 表示日期是否为季度初。 |
Series.dt.is_quarter_end | 表示日期是否为季度末。 |
Series.dt.is_year_start | 表示日期是否为年初。 |
Series.dt.is_year_end | 指示日期是否为年底。 |
Series.dt.is_leap_year | 布尔值指示日期是否属于闰年。 |
Series.dt.daysinmonth | 该月的天数。 |
Series.dt.days_in_month | 该月的天数。 |
Series.dt.tz | 返回时区。 |
Series.dt.freq | 返回此 PeriodArray 的频率对象。 |
Series.dt.unit |
日期时间方法
Series.dt.isocalendar () | 根据 ISO 8601 标准计算年、周和日。 |
---|---|
Series.dt.to_period (*args, **kwargs) | 转换为特定频率的 PeriodArray/PeriodIndex。 |
Series.dt.to_pydatetime () | (已弃用) 将数据返回为 datetime.datetime 对象的数组。 |
Series.dt.tz_localize (*args, **kwargs) | 将 tz-naive 的日期时间数组/索引本地化为 tz-aware 的日期时间数组/索引。 |
Series.dt.tz_convert (*args, **kwargs) | 将来自一个时区的 tz-aware 的日期时间数组/索引转换为另一个时区。 |
Series.dt.normalize (*args, **kwargs) | 将时间转换为午夜。 |
Series.dt.strftime (*args, **kwargs) | 使用指定的日期格式转换为索引。 |
Series.dt.round (*args, **kwargs) | 对数据执行指定频率的四舍五入操作。 |
Series.dt.floor (*args, **kwargs) | 对数据执行指定频率的向下取整操作。 |
Series.dt.ceil (*args, **kwargs) | 对数据执行向上取整操作到指定的频率。 |
Series.dt.month_name (*args, **kwargs) | 返回指定语言环境下的月份名称。 |
Series.dt.day_name (*args, **kwargs) | 返回指定语言环境下的星期几名称。 |
Series.dt.as_unit (*args, **kwargs) |
时期属性
Series.dt.qyear | |
---|---|
Series.dt.start_time | 获取周期开始时的时间戳。 |
Series.dt.end_time | 获取周期结束时的时间戳。 |
时间差属性
Series.dt.days | 每个元素的天数。 |
---|---|
Series.dt.seconds | 每个元素的秒数(>= 0 且小于 1 天)。 |
Series.dt.microseconds | 每个元素的微秒数(>= 0 且小于 1 秒)。 |
Series.dt.nanoseconds | 每个元素的纳秒数(>= 0 且小于 1 微秒)。 |
Series.dt.components | 返回时间差的各个组成部分的数据框。 |
Series.dt.unit |
时间差方法
Series.dt.to_pytimedelta () | 返回一个由本地datetime.timedelta 对象组成的数组。 |
---|---|
Series.dt.total_seconds (*args, **kwargs) | 返回每个元素表示的总持续时间(以秒为单位)。 |
Series.dt.as_unit (*args, **kwargs) |
日期时间属性
Series.dt.date | 返回带有 python datetime.date 对象的 numpy 数组。 |
---|---|
Series.dt.time | 返回带有datetime.time 对象的 numpy 数组。 |
Series.dt.timetz | 返回带有时区的datetime.time 对象的 numpy 数组。 |
Series.dt.year | 日期的年份。 |
Series.dt.month | 月份,一月为 1,十二月为 12。 |
Series.dt.day | 日期的日。 |
Series.dt.hour | 日期的小时。 |
Series.dt.minute | 日期的分钟。 |
Series.dt.second | 日期的秒。 |
Series.dt.microsecond | 日期的微秒。 |
Series.dt.nanosecond | 日期的纳秒。 |
Series.dt.dayofweek | 一周中的第几天,星期一为 0,星期日为 6。 |
Series.dt.day_of_week | 一周中的第几天,星期一为 0,星期日为 6。 |
Series.dt.weekday | 一周中的第几天,星期一为 0,星期日为 6。 |
Series.dt.dayofyear | 一年中的第几天。 |
Series.dt.day_of_year | 一年中的第几天。 |
Series.dt.days_in_month | 月份的天数。 |
Series.dt.quarter | 日期的季度。 |
Series.dt.is_month_start | 指示日期是否为一个月的第一天。 |
Series.dt.is_month_end | 指示日期是否为一个月的最后一天。 |
Series.dt.is_quarter_start | 指示日期是否为一个季度的第一天。 |
Series.dt.is_quarter_end | 指示日期是否为一个季度的最后一天。 |
Series.dt.is_year_start | 指示日期是否为一年的第一天。 |
Series.dt.is_year_end | 指示日期是否为一年的最后一天。 |
Series.dt.is_leap_year | 布尔指示器,指示日期是否属于闰年。 |
Series.dt.daysinmonth | 月份中的天数。 |
Series.dt.days_in_month | 月份中的天数。 |
Series.dt.tz | 返回时区。 |
Series.dt.freq | 返回此 PeriodArray 的频率对象。 |
Series.dt.unit |
日期时间方法
Series.dt.isocalendar () | 根据 ISO 8601 标准计算年、周和日。 |
---|---|
Series.dt.to_period (*args, **kwargs) | 在特定频率下转换为 PeriodArray/PeriodIndex。 |
Series.dt.to_pydatetime () | (已弃用) 将数据返回为 datetime.datetime 对象数组。 |
Series.dt.tz_localize (*args, **kwargs) | 将时区无关的日期时间数组/索引本地化为时区感知的日期时间数组/索引。 |
Series.dt.tz_convert (*args, **kwargs) | 将来自一个时区的时区感知的日期时间数组/索引转换为另一个时区。 |
Series.dt.normalize (*args, **kwargs) | 将时间转换为午夜。 |
Series.dt.strftime (*args, **kwargs) | 使用指定的日期格式转换为索引。 |
Series.dt.round (*args, **kwargs) | 对数据执行四舍五入操作,以指定的频率为准。 |
Series.dt.floor (*args, **kwargs) | 对数据执行向下取整操作,以指定的频率为准。 |
Series.dt.ceil (*args, **kwargs) | 对数据执行向上取整操作,以指定的频率为准。 |
Series.dt.month_name (*args, **kwargs) | 返回指定区域设置的月份名称。 |
Series.dt.day_name (*args, **kwargs) | 返回指定区域设置的星期几名称。 |
Series.dt.as_unit (*args, **kwargs) |
时间周期属性
Series.dt.qyear | |
---|---|
Series.dt.start_time | 获取周期开始的时间戳。 |
Series.dt.end_time | 获取周期结束的时间戳。 |
时间差属性
Series.dt.days | 每个元素的天数。 |
---|---|
Series.dt.seconds | 每个元素的秒数(>= 0 并且小于 1 天)。 |
Series.dt.microseconds | 每个元素的微秒数(>= 0 并且小于 1 秒)。 |
Series.dt.nanoseconds | 每个元素的纳秒数(>= 0 且小于 1 微秒)。 |
Series.dt.components | 返回时间增量的各个组件的数据框。 |
Series.dt.unit |
时间增量方法
Series.dt.to_pytimedelta () | 返回一个由本地datetime.timedelta 对象组成的数组。 |
---|---|
Series.dt.total_seconds (*args, **kwargs) | 返回每个元素表示的总持续时间(以秒为单位)。 |
Series.dt.as_unit (*args, **kwargs) |
字符串处理
Series.str
可用于访问系列的���作为字符串并应用多种方法。这些可以像Series.str.<function/property>
这样访问。
Series.str.capitalize () | 将系列/索引中的字符串转换为大写。 |
---|---|
Series.str.casefold () | 将系列/索引中的字符串转换为折叠大小写形式。 |
Series.str.cat ([others, sep, na_rep, join]) | 使用给定的分隔符连接系列/索引中的字符串。 |
Series.str.center (width[, fillchar]) | 在系列/索引中的字符串的左侧和右侧填充。 |
Series.str.contains (pat[, case, flags, na, …]) | 测试模式或正则表达式是否包含在系列或索引的字符串中。 |
Series.str.count (pat[, flags]) | 计算系列/索引中每个字符串中模式的出现次数。 |
Series.str.decode (encoding[, errors]) | 使用指定的编码对系列/索引中的字符字符串进行解码。 |
Series.str.encode (encoding[, errors]) | 使用指定的编码对系列/索引中的字符字符串进行编码。 |
Series.str.endswith (pat[, na]) | 测试每个字符串元素的结尾是否与模式��配。 |
Series.str.extract (pat[, flags, expand]) | 在 DataFrame 中提取正则表达式 pat 中的捕获组作为列。 |
Series.str.extractall (pat[, flags]) | 在 DataFrame 中提取正则表达式 pat 中的捕获组作为列。 |
Series.str.find (sub[, start, end]) | 返回 Series/Index 中每个字符串中的最低索引。 |
Series.str.findall (pat[, flags]) | 在 Series/Index 中查找模式或正则表达式的所有出现。 |
Series.str.fullmatch (pat[, case, flags, na]) | 确定每个字符串是否完全匹配正则表达式。 |
Series.str.get (i) | 从每个组件中提取指定位置或指定键的元素。 |
Series.str.index (sub[, start, end]) | 返回 Series/Index 中每个字符串的最低索引。 |
Series.str.join (sep) | 使用传递的分隔符将 Series/Index 中作为元素包含的列表连接起来。 |
Series.str.len () | 计算 Series/Index 中每个元素的长度。 |
Series.str.ljust (width[, fillchar]) | 在 Series/Index 中字符串的右侧填充。 |
Series.str.lower () | 将 Series/Index 中的字符串转换为小写。 |
Series.str.lstrip ([to_strip]) | 移除前导字符。 |
Series.str.match (pat[, case, flags, na]) | 确定每个字符串是否以正则表达式的匹配开头。 |
Series.str.normalize (form) | 返回 Series/Index 中字符串的 Unicode 标准形式。 |
Series.str.pad | 在 Series/Index 中的字符串中填充到指定宽度。 |
Series.str.partition | 在第一个 sep 出现的位置拆分字符串。 |
Series.str.removeprefix | 从对象系列中删除前缀。 |
Series.str.removesuffix | 从对象系列中删除后缀。 |
Series.str.repeat | 在 Series 或 Index 中复制每个字符串。 |
Series.str.replace | 在 Series/Index 中替换每个模式/正则表达式的每个出现。 |
Series.str.rfind | 返回 Series/Index 中每个字符串中的最高索引。 |
Series.str.rindex | 返回 Series/Index 中每个字符串中的最高索引。 |
Series.str.rjust | 在 Series/Index 中的字符串左侧填充。 |
Series.str.rpartition | 在最后一个 sep 出现的位置拆分字符串。 |
Series.str.rstrip | 移除字符串尾部的字符。 |
Series.str.slice | 从 Series 或 Index 中的每个元素中切割子字符串。 |
Series.str.slice_replace | 用另一个值替换字符串的位置切片。 |
Series.str.split | 在给定的分隔符/定界符周围分割字符串。 |
Series.str.rsplit | 在给定的分隔符/定界符周围分割字符串。 |
Series.str.startswith | 测试每个字符串元素的开头是否与模式匹配。 |
Series.str.strip ([to_strip]) | 移除字符串的开头和结尾字符。 |
Series.str.swapcase () | 将 Series/Index 中的字符串转换为大小写互换。 |
Series.str.title () | 将 Series/Index 中的字符串转换为标题格式。 |
Series.str.translate (table) | 通过给定的映射表映射字符串中的所有字符。 |
Series.str.upper () | 将 Series/Index 中的字符串转换为大写。 |
Series.str.wrap (width, **kwargs) | 在指定的行宽度处将 Series/Index 中的字符串换行。 |
Series.str.zfill (width) | 通过在 Series/Index 中的字符串前面添加 ‘0’ 字符来填充字符串。 |
Series.str.isalnum () | 检查每个字符串中的所有字符是否都是字母数字。 |
Series.str.isalpha () | 检查每个字符串中的所有字符是否都是字母。 |
Series.str.isdigit () | 检查每个字符串中的所有字符是否都是数字。 |
Series.str.isspace () | 检查每个字符串中的所有字符是否都是空白字符。 |
Series.str.islower () | 检查每个字符串中的所有字符是否都是小写。 |
Series.str.isupper () | 检查每个字符串中的所有字符是否都是大写。 |
Series.str.istitle () | 检查每个字符串中的所有字符是否都是标题格式。 |
Series.str.isnumeric () | 检查每个字符串中的所有字符是否都是数字。 |
Series.str.isdecimal () | 检查每个字符串中的所有字符是否都是十进制数。 |
Series.str.get_dummies ([sep]) | 返回 Series 的虚拟/指示变量的 DataFrame。 |
分类访问器
类别特定的方法和属性可在 Series.cat
访问器下找到。
Series.cat.categories | 此分类的类别。 |
---|---|
Series.cat.ordered | 类别是否具有有序关系。 |
Series.cat.codes | 返回代码的 Series 以及索引。 |
Series.cat.rename_categories (*args, **kwargs) | 重命名类别。 |
Series.cat.reorder_categories (*args, **kwargs) | 根据新类别重新排序类别。 |
Series.cat.add_categories (*args, **kwargs) | 添加新类别。 |
Series.cat.remove_categories (*args, **kwargs) | 删除指定的类别。 |
Series.cat.remove_unused_categories (*args, …) | 删除未使用的类别。 |
Series.cat.set_categories (*args, **kwargs) | 将类别设置为指定的新类别。 |
Series.cat.as_ordered (*args, **kwargs) | 将分类设置为有序。 |
Series.cat.as_unordered (*args, **kwargs) | 将分类设置为无序。 |
稀疏访问器
稀疏特定的方法和属性可在 Series.sparse
访问器下找到。
Series.sparse.npoints | 非 fill_value 点的数量。 |
---|---|
Series.sparse.density | 非 fill_value 点的百分比,以小数表示。 |
Series.sparse.fill_value | 数据中的 fill_value 元素不会被存储。 |
Series.sparse.sp_values | 包含非 fill_value 值的 ndarray。 |
Series.sparse.from_coo (A[, dense_index]) | 从 scipy.sparse.coo_matrix 创建具有稀疏值的 Series。 |
Series.sparse.to_coo ([row_levels, …]) | 从具有 MultiIndex 的 Series 创建一个 scipy.sparse.coo_matrix。 |
列表访问器
Arrow 列表数据类型的特定方法和属性可在 Series.list
访问器下找到。
Series.list.flatten () | 展平列表值。 |
---|---|
Series.list.len () | 返回 Series 中每个列表的长度。 |
Series.list.__getitem__ (key) | 在 Series 中索引或切片列表。 |
结构访问器
Arrow 结构数据类型的特定方法和属性可在 Series.struct
访问器下找到。
Series.struct.dtypes | 返回结构的每个子字段的 dtype 对象。 |
---|---|
Series.struct.field (name_or_index) | 将结构的子字段提取为 Series。 |
Series.struct.explode () | 将结构的所有子字段提取为 DataFrame。 |
标志
标志指的是 pandas 对象的属性。数据集的属性(如记录日期、访问的 URL 等)应存储在 Series.attrs
中。
Flags (obj, *, allows_duplicate_labels) | 适用于 pandas 对象的标志。 |
---|
元数据
Series.attrs
是一个用于存储此 Series 的全局元数据的字典。
警告
Series.attrs
被视为实验性质,可能会在不经警告的情况下更改。
Series.attrs | 此数据集的全局属性字典。 |
---|
绘图
Series.plot
既是一个可调用方法,也是特定绘图方法的命名空间属性,形式为 Series.plot.<kind>
。
Series.plot ([kind, ax, figsize, …]) | Series 绘图访问器和方法 |
---|---|
Series.plot.area ([x, y, stacked]) | 绘制堆叠面积图。 |
Series.plot.bar ([x, y]) | 绘制垂直条形图。 |
Series.plot.barh ([x, y]) | 绘制水平条形图。 |
Series.plot.box ([by]) | 绘制 DataFrame 列的箱线图。 |
Series.plot.density ([bw_method, ind]) | 使用高斯核生成核密度估计图。 |
Series.plot.hist ([by, bins]) | 绘制 DataFrame 列的直方图。 |
Series.plot.kde ([bw_method, ind]) | 使用高斯核生成核密度估计图。 |
Series.plot.line ([x, y]) | 将 Series 或 DataFrame 绘制为折线图。 |
Series.plot.pie (**kwargs) | 生成饼图。 |
Series.hist ([by, ax, grid, xlabelsize, …]) | 使用 matplotlib 绘制输入序列的直方图。 |
序列化 / IO / 转换
Series.to_pickle (path, *[, compression, …]) | 将对象序列化(Pickle)到文件。 |
---|---|
Series.to_csv ([path_or_buf, sep, na_rep, …]) | 将对象写入逗号分隔值(csv)文件。 |
Series.to_dict (*[, into]) | 将 Series 转换为 {标签 -> 值} 字典或类似字典的对象。 |
Series.to_excel (excel_writer, *[, …]) | 将对象写入 Excel 表格。 |
Series.to_frame ([name]) | 将 Series 转换为 DataFrame。 |
Series.to_xarray () | 从 pandas 对象返回一个 xarray 对象。 |
Series.to_hdf (path_or_buf, *, key[, mode, …]) | 使用 HDFStore 将包含的数据写入 HDF5 文件。 |
Series.to_sql (name, con, *[, schema, …]) | 将存储在 DataFrame 中的记录写入 SQL 数据库。 |
Series.to_json ([path_or_buf, orient, …]) | 将对象转换为 JSON 字符串。 |
Series.to_string ([buf, na_rep, …]) | 呈现 Series 的字符串表示。 |
Series.to_clipboard (*[, excel, sep]) | 将对象复制到系统剪贴板。 |
Series.to_latex ([buf, columns, header, …]) | 将对象呈现为 LaTeX 表格、长表格或嵌套表格。 |
Series.to_markdown ([buf, mode, index, …]) | 以 Markdown 友好的格式打印 Series。 |