标量#

数组#

For most data types, pandas uses NumPy arrays as the concrete objects contained with a Index, Series, or DataFrame.

dtypes

pandas and third-party libraries can extend NumPy’s type system (see 扩展类型). The top-level array() method can be used to create a new array, which may be stored in a Series, Index, or as a column in a DataFrame.

array (data[, dtype, copy])

创建一个数组。

PyArrow#

警告

此功能为实验性功能,API 可能在未来版本中修改,恕不另行通知。

arrays.ArrowExtensionArraypyarrow.ChunkedArray 支持,并使用 pyarrow.DataType 而非 NumPy 数组和数据类型。 arrays.ArrowExtensionArray.dtype 是一个 ArrowDtype

Pyarrow 提供了与 NumPy 类似的数组和 data type 支持,包括所有数据类型的头等空值支持、不可变性等。

下表显示了 pandas 识别的 Pyarrow 支持的(pa)、pandas 扩展和 NumPy(np)的等效类型。下表中的 Pyarrow 支持的类型需要传递给 ArrowDtype 才能被 pandas 识别,例如 pd.ArrowDtype(pa.bool_())

备注

Pyarrow 支持的字符串由 pd.StringDtype("pyarrow")pd.ArrowDtype(pa.string()) 提供。 pd.StringDtype("pyarrow")string section 中有介绍,当指定字符串别名 "string[pyarrow]" 时会被返回。 pd.ArrowDtype(pa.string()) 通常与 ArrowDtype 的其他类型具有更好的互操作性。

arrays.ArrowExtensionArray 中的单个值存储为 PyArrow 对象,但标量值会以与数据类型对应的 Python 标量形式**返回**,例如,PyArrow int64 将返回为 Python int,缺失值返回为 NA

arrays.ArrowExtensionArray (values)

基于 PyArrow ChunkedArray 的 Pandas 扩展数组。

ArrowDtype (pyarrow_dtype)

Arrow 的数据类型的扩展类型。

更多信息,请参阅 PyArrow user guide

日期时间#

NumPy 无法原生表示带时区的日期时间。pandas 通过 arrays.DatetimeArray 扩展数组来支持此功能,该数组可以包含带时区或不带时区的日期时间值。

Timestampdatetime.datetime 的子类,是 pandas 用于带时区或不带时区的日期时间数据的标量类型。NaT 是日期时间数据的缺失值。

Timestamp ([ts_input, year, month, day, ...])

Python datetime.datetime 对象的 Pandas 替代。

属性#

Timestamp.asm8 

返回 numpy datetime64 格式(以纳秒为单位)。

Timestamp.day 

Timestamp.dayofweek 

返回星期几。

Timestamp.day_of_week 

返回星期几。

Timestamp.dayofyear 

返回一年中的第几天。

Timestamp.day_of_year 

返回一年中的第几天。

Timestamp.days_in_month 对象的月份天数

返回月份中的天数。

Timestamp.daysinmonth 对象的月份天数

返回月份中的天数。

Timestamp.fold 对象的折叠属性

Timestamp.hour 对象的时

Timestamp.is_leap_year 对象是否为闰年

如果年份是闰年,则返回 True。

Timestamp.is_month_end 对象是否为月末

检查日期是否为月份的最后一天。

Timestamp.is_month_start 对象是否为月初

检查日期是否为月份的第一天。

Timestamp.is_quarter_end 对象是否为季末

检查日期是否为季度的最后一天。

Timestamp.is_quarter_start 对象是否为季初

检查日期是否为季度的第一天。

Timestamp.is_year_end 对象是否为年末

如果日期是该年的最后一天,则返回 True。

Timestamp.is_year_start 对象是否为年初

如果日期是该年的第一天,则返回 True。

Timestamp.max 对象的最大值

Timestamp.microsecond 对象的微秒

Timestamp.min 对象的最小值

Timestamp.minute 对象的分钟

Timestamp.month 对象的月份

Timestamp.nanosecond 对象的纳秒

Timestamp.quarter 对象的季度

返回一年中的季度。

Timestamp.resolution 对象的精度

Timestamp.second 对象的秒

Timestamp.tz 对象的时区

tzinfo 的别名。

Timestamp.tzinfo 对象的时区信息

Timestamp.unit 对象的单位

与 self._creso 关联的缩写。

Timestamp.value 对象的数值

Timestamp.week 对象的周

返回一年中的周数。

Timestamp.weekofyear 对象的年中的周

返回一年中的周数。

Timestamp.year 对象的年份

Methods#

Timestamp.as_unit (unit[, round_ok]) 将时间戳转换为指定的单位

将底层的 int64 表示转换为给定的单位。

Timestamp.astimezone (tz) 将时间戳转换为指定的时区

将时区感知的 Timestamp 转换为另一个时区。

Timestamp.ceil (freq[, ambiguous, nonexistent]) 向上取整到指定频率

返回一个新的 Timestamp,其值向上取整到此分辨率。

Timestamp.combine (date, time) 组合日期和时间创建时间戳

组合日期和时间,得到具有相同日期和时间字段的 datetime。

Timestamp.ctime () 返回对象的 C 时间表示

返回 ctime() 风格的字符串。

Timestamp.date () 返回对象的日期部分

返回具有相同年、月、日的 date 对象。

Timestamp.day_name ([locale]) 返回对象是星期几的名称

返回具有指定区域设置的 Timestamp 的日期名称。

Timestamp.dst () 返回对象的夏令时偏移量

返回夏令时 (DST) 调整。

Timestamp.floor (freq[, ambiguous, nonexistent]) 向下取整到指定频率

返回一个新的 Timestamp,其值向下取整到此分辨率。

Timestamp.fromordinal (ordinal[, tz]) 从序数创建时间戳

从序数(proleptic Gregorian ordinal)构造时间戳。

Timestamp.fromtimestamp (ts) 从 POSIX 时间戳创建时间戳

根据 POSIX 时间戳将 timestamp[, tz] 转换为 tz 的本地时间。

Timestamp.isocalendar () 返回 ISO 日历信息

返回一个命名元组,包含 ISO 年、周数和星期几。

Timestamp.isoformat ([sep, timespec]) 返回 ISO 格式字符串

按 ISO 8601 格式返回时间。

Timestamp.isoweekday () 返回 ISO 星期几

返回日期所代表的星期几。

Timestamp.month_name ([locale]) 返回对象是几月的名称

使用指定的 locale 返回 Timestamp 的月份名称。

Timestamp.normalize () 将时间戳归一化到午夜

将 Timestamp 规范化到午夜,保留 tz 信息。

Timestamp.now ([tz]) 获取当前时间戳

返回新的 Timestamp 对象,表示 tz 的当前本地时间。

Timestamp.replace ([year, month, day, hour, ...]) 替换部分时间戳的组件

实现 datetime.replace,处理纳秒。

Timestamp.round (freq[, ambiguous, nonexistent]) 四舍五入到指定频率

将 Timestamp 四舍五入到指定的精度。

Timestamp.strftime (format) 根据指定格式进行格式化

返回 Timestamp 的格式化字符串。

Timestamp.strptime (string, format) 将字符串根据指定格式解析为时间戳

函数未实现。

Timestamp.time () 返回对象的时间部分

返回时间对象,具有相同的时间,但 tzinfo=None。

Timestamp.timestamp () 返回对象的 POSIX 时间戳

返回 POSIX 时间戳(float 类型)。

Timestamp.timetuple () 返回对象的 time.struct_time 表示

返回时间元组,与 time.localtime() 兼容。

Timestamp.timetz () 返回对象的带时区 time.struct_time 表示

返回时间对象,具有相同的时间和 tzinfo。

Timestamp.to_datetime64 () 转换为 NumPy datetime64 对象

返回具有相同精度的 numpy.datetime64 对象。

Timestamp.to_numpy ([dtype, copy]) 转换为 NumPy 数组

将 Timestamp 转换为 NumPy datetime64。

Timestamp.to_julian_date () 转换为儒略日数

将 TimeStamp 转换为儒略日。

Timestamp.to_period ([freq]) 转换为 Period 对象

返回此时间戳是其观测值的周期。

Timestamp.to_pydatetime ([warn]) 转换为 Python datetime 对象

将 Timestamp 对象转换为本地 Python datetime 对象。

Timestamp.today ([tz]) 获取当前日期时间戳

返回本地时区的当前时间。

Timestamp.toordinal () 返回对象的序数值

返回序数(proleptic Gregorian ordinal)。

Timestamp.tz_convert (tz) 将时间戳的时区转换为指定时区

将时区感知的 Timestamp 转换为另一个时区。

Timestamp.tz_localize (tz[, ambiguous, ...]) 本地化时间戳的时区

将 Timestamp 本地化到某个时区。

Timestamp.tzname () 返回对象的时区名称

返回时区名称。

Timestamp.utcfromtimestamp (ts)

从 POSIX 时间戳构造一个时区感知的 UTC 日期时间。

Timestamp.utcnow ()

返回一个新的 Timestamp,表示 UTC 的日期和时间。

Timestamp.utcoffset ()

返回 UTC 偏移量。

Timestamp.utctimetuple ()

返回 UTC 时间元组,与 time.localtime() 兼容。

Timestamp.weekday ()

返回日期所代表的星期几。

一组时间戳可以存储在 arrays.DatetimeArray 中。对于带有时区的数据,arrays.DatetimeArray.dtypeDatetimeTZDtype 。对于不带时区的数据,则使用 np.dtype("datetime64[ns]")

如果数据带有时区,则数组中的每个值都必须具有相同的时区。

arrays.DatetimeArray (values[, dtype, freq, copy])

pandas 的时区无关或时区感知日期时间数据的扩展数组。

DatetimeTZDtype ([unit, tz])

用于时区感知日期时间数据的 ExtensionDtype。

Period#

NumPy 可以原生表示时间差。pandas 提供了 Timedelta 来与 Timestamp 对称。NaT 是 timedelta 数据的缺失值。

Timedelta ([value, unit])

表示一个持续时间,两个日期或时间之间的差值。

属性#

Timedelta.asm8 

返回一个 numpy timedelta64 数组标量视图。

Timedelta.components 

返回一个类似命名元组的 components。

Timedelta.days 

返回 timedelta 的天数。

Timedelta.max 

Timedelta.microseconds 

Timedelta.min 

Timedelta.nanoseconds 

返回纳秒数(n),其中 0 <= n < 1 微秒。

Timedelta.resolution 

Timedelta.seconds 

返回 timedelta 的总小时、分钟和秒数(以秒为单位)。

Timedelta.unit 

Timedelta.value 

Timedelta.view (dtype)

数组视图兼容性。

Methods#

Timedelta.as_unit (unit[, round_ok])

将底层的 int64 表示转换为给定的单位。

Timedelta.ceil (freq)

返回一个新的、向上取整到此分辨率的 Timedelta。

Timedelta.floor (freq)

返回一个新的、向下取整到此分辨率的 Timedelta。

Timedelta.isoformat ()

将 Timedelta 格式化为 ISO 8601 Duration。

Timedelta.round (freq)

将 Timedelta 四舍五入到指定的分辨率。

Timedelta.to_pytimedelta ()

将 pandas Timedelta 对象转换为 python 的 datetime.timedelta 对象。

Timedelta.to_timedelta64 ()

返回一个精度为 'ns' 的 numpy.timedelta64 对象。

Timedelta.to_numpy ([dtype, copy])

将 Timedelta 转换为 NumPy timedelta64。

Timedelta.total_seconds ()

持续时间内的总秒数。

一组 Timedelta 可以存储在 TimedeltaArray 中。

arrays.TimedeltaArray (values[, dtype, freq, ...])

Pandas 扩展数组,用于 timedelta 数据。

Period#

pandas 使用 Period 对象来表示时间跨度。

Period#

Period ([value, freq, ordinal, year, month, ...])

Represents a period of time.

属性#

Period.day 

获取 Period 所属月份中的日期。

Period.dayofweek 

Period 所属星期几,星期一=0,星期日=6。

Period.day_of_week 

Period 所属星期几,星期一=0,星期日=6。

Period.dayofyear 

返回一年中的第几天。

Period.day_of_year 

返回一年中的第几天。

Period.days_in_month 

获取 Period 所属月份的总天数。

Period.daysinmonth 

获取 Period 所属月份的总天数。

Period.end_time 

获取 Period 结束时间的 Timestamp。

Period.freq 

Period.freqstr 

返回频率的字符串表示。

Period.hour 

获取 Period 的日小时部分。

Period.is_leap_year 

如果 Period 的年份是闰年,则返回 True。

Period.minute 

获取 Period 的小时分钟部分。

Period.month 

返回 Period 所属的月份。

Period.ordinal 

Period.quarter 

返回 Period 所属的季度。

Period.qyear 

Fiscal year the Period lies in according to its starting-quarter.

Period.second 

获取 Period 的秒部分。

Period.start_time 

获取 Period 开始时间的 Timestamp。

Period.week 

获取 Period 是一年中的第几周。

Period.weekday 

Period 所属星期几,星期一=0,星期日=6。

Period.weekofyear 

获取 Period 是一年中的第几周。

Period.year 

返回 Period 所属的年份。

Methods#

Period.asfreq (freq[, how])

将 Period 转换为指定的频率,在区间开始或结束处。

Period.now (freq)

返回当前日期的 Period。

Period.strftime (fmt)

返回 Period 的格式化字符串表示。

Period.to_timestamp ([freq, how])

返回 Period 的 Timestamp 表示。

:class:`Period 的集合可以存储在 :class:`arrays.PeriodArray 中。:class:`arrays.PeriodArray 中的每个 period 必须具有相同的 freq

arrays.PeriodArray (values[, dtype, freq, copy])

用于存储 Period 数据的 Pandas 扩展数组。

PeriodDtype (freq)

Period 数据的 ExtensionDtype。

可空整数#

任意区间可以表示为 :class:`Interval 对象。

Interval 

Immutable object implementing an Interval, a bounded slice-like interval.

属性#

Interval.closed 

String describing the inclusive side the intervals.

Interval.closed_left 

Check if the interval is closed on the left side.

Interval.closed_right 

Check if the interval is closed on the right side.

Interval.is_empty 

Indicates if an interval is empty, meaning it contains no points.

Interval.left 

Left bound for the interval.

Interval.length 

Return the length of the Interval.

Interval.mid 

Return the midpoint of the Interval.

Interval.open_left 

Check if the interval is open on the left side.

Interval.open_right 

Check if the interval is open on the right side.

Interval.overlaps (other)

Check whether two Interval objects overlap.

Interval.right 

Right bound for the interval.

区间的集合可以存储在 :class:`arrays.IntervalArray 中。

arrays.IntervalArray (data[, closed, dtype, ...])

Pandas 数组,用于存储同侧闭合的区间数据。

IntervalDtype ([subtype, closed])

用于 Interval 数据的 ExtensionDtype。

可空整数#

:class:`numpy.ndarray 无法原生表示带缺失值的整数数据。pandas 通过 :class:`arrays.IntegerArray 提供此功能。

arrays.IntegerArray (values, mask[, copy])

整数(可选缺失)值数组。

Int8Dtype ()

An ExtensionDtype for int8 integer data.

Int16Dtype ()

An ExtensionDtype for int16 integer data.

Int32Dtype ()

An ExtensionDtype for int32 integer data.

Int64Dtype ()

An ExtensionDtype for int64 integer data.

UInt8Dtype ()

用于 uint8 整数数据的 ExtensionDtype。

UInt16Dtype ()

用于 uint16 整数数据的 ExtensionDtype。

UInt32Dtype ()

用于 uint32 整数数据的 ExtensionDtype。

UInt64Dtype ()

用于 uint64 整数数据的 ExtensionDtype。

可空浮点数#

arrays.FloatingArray (values, mask[, copy])

浮点(可选缺失)值数组。

Float32Dtype ()

An ExtensionDtype for float32 data.

Float64Dtype ()

An ExtensionDtype for float64 data.

分类数据#

pandas 定义了一种自定义数据类型,用于表示只能取有限、固定集合值的数据。:class:`Categorical 的 dtype 可以通过 :class:`CategoricalDtype 来描述。

CategoricalDtype ([categories, ordered])

具有类别和有序性的类别数据的类型。

CategoricalDtype.categories 

一个包含允许的唯一类别的 Index

CategoricalDtype.ordered 

分类是否具有有序关系。

分类数据可以存储在 :class:`pandas.Categorical 中。

Categorical (values[, categories, ordered, ...])

以经典的 R / S-plus 风格表示分类变量。

当您已经有了类别和整数代码时,可以使用替代的 :meth:`Categorical.from_codes 构造函数:

Categorical.from_codes (codes[, categories, ...])

从代码和分类或 dtype 创建 Categorical 类型。

dtype 信息可以在 :class:`Categorical 上获取。

Categorical.dtype 

此实例 the CategoricalDtype

Categorical.categories 

此分类的分类。

Categorical.ordered 

分类是否具有有序关系。

Categorical.codes 

此分类索引的分类代码。

np.asarray(categorical) 通过实现数组接口工作。请注意,这会将 :class:`Categorical 转换回 NumPy 数组,因此类别和顺序信息不会被保留!

Categorical.__array__ ([dtype, copy])

numpy 数组接口。

:class:`Categorical 可以存储在 :class:`Series:class:`DataFrame 中。要创建 dtype 为 category 的 Series,请使用 cat = s.astype(dtype)Series(..., dtype=dtype),其中 dtype 可以是

  • 字符串 'category'

  • :class:`CategoricalDtype 的实例。

如果 :class:`Series 的 dtype 是 :class:`CategoricalDtype ,则可以使用 Series.cat 来更改分类数据。更多信息请参见 分类 accessor

稀疏#

重复多次的单个值(例如 0NaN)的数据可以高效地存储为 :class:`arrays.SparseArray

arrays.SparseArray (data[, sparse_index, ...])

用于存储稀疏数据的 ExtensionArray。

SparseDtype (dtypefill_value

存储在 SparseArray 中的数据的 Dtype。

如果 Series 包含稀疏值,则可以使用 Series.sparse 访问器来访问特定于稀疏的属性和方法。更多信息请参阅 稀疏访问器 (Sparse accessor)the user guide

PyArrow#

在处理文本数据时,其中每个有效元素都是字符串或缺失值,我们建议使用 StringDtype (别名 "string")。

arrays.StringArray (values`[, `copy])

字符串数据的扩展数组。

arrays.ArrowStringArray (values

pyarrow.ChunkedArray 中字符串数据的扩展数组。

StringDtype ([storage`[, `na_value]])

字符串数据的扩展 Dtype。

对于由 Series 支持的 arrays.StringArray ,可以使用 Series.str 访问器。更多信息请参阅 字符串处理

PyArrow#

布尔数据类型(别名 “boolean”)支持存储包含缺失值的布尔数据(TrueFalse),这对于 bool numpy.ndarray 是不可能的。

arrays.BooleanArray (valuesmask`[, `copy])

带有缺失值的布尔(True/False)数据数组。

BooleanDtype ()

布尔数据的扩展类型。

工具#

构造函数#

api.types.union_categoricals (`to_union`[, ...])

组合列表式的 Categorical 对象,并联合类别。

api.types.infer_dtype (value`[, `skipna])

返回标量或类列表值的类型标签。

api.types.pandas_dtype (dtype

将输入转换为仅 pandas 的 dtype 对象或 numpy 的 dtype 对象。

数据类型内省#

api.types.is_any_real_numeric_dtype (arr_or_dtype

检查提供的数组或 dtype 是否为实数 dtype。

api.types.is_bool_dtype (arr_or_dtype

检查提供的数组或 dtype 是否为布尔 dtype。

api.types.is_categorical_dtype (arr_or_dtype

(已弃用)检查数组状对象或数据类型是否是 Categorical 数据类型。

api.types.is_complex_dtype (arr_or_dtype

检查提供的数组或 dtype 是否为复数 dtype。

api.types.is_datetime64_any_dtype (arr_or_dtype

检查提供的数组或 dtype 是否为 datetime64 dtype。

api.types.is_datetime64_dtype (arr_or_dtype

检查类数组或 dtype 是否为 datetime64 dtype。

api.types.is_datetime64_ns_dtype (arr_or_dtype

检查提供的数组或 dtype 是否为 datetime64[ns] dtype。

api.types.is_datetime64tz_dtype (arr_or_dtype

(已弃用)检查数组状对象或数据类型是否是 DatetimeTZDtype 数据类型。

api.types.is_extension_array_dtype (arr_or_dtype

检查一个对象是否为 pandas 扩展数组类型。

api.types.is_float_dtype (arr_or_dtype

检查提供的数组或 dtype 是否为浮点数 dtype。

api.types.is_int64_dtype (arr_or_dtype

(已弃用)检查提供的数组或数据类型是否是 int64 数据类型。

api.types.is_integer_dtype (arr_or_dtype

检查提供的数组或 dtype 是否为整数 dtype。

api.types.is_interval_dtype (arr_or_dtype

(已弃用)检查数组状对象或数据类型是否是 Interval 数据类型。

api.types.is_numeric_dtype (arr_or_dtype

检查提供的数组或 dtype 是否为数字 dtype。

api.types.is_object_dtype (arr_or_dtype

检查数组或 dtype 是否为 object dtype。

api.types.is_period_dtype (arr_or_dtype

(已弃用)检查数组状对象或数据类型是否是 Period 数据类型。

api.types.is_signed_integer_dtype (arr_or_dtype

检查提供的数组或 dtype 是否为有符号整数 dtype。

api.types.is_string_dtype (arr_or_dtype

检查提供的数组或 dtype 是否为字符串 dtype。

api.types.is_timedelta64_dtype (arr_or_dtype

检查类数组对象或 dtype 是否为 timedelta64 dtype。

api.types.is_timedelta64_ns_dtype (arr_or_dtype

检查提供的数组或 dtype 是否为 timedelta64[ns] dtype。

api.types.is_unsigned_integer_dtype (arr_or_dtype

检查提供的数组或 dtype 是否为无符号整数 dtype。

api.types.is_sparse (arr

(已弃用)检查数组状对象是否是 1-D pandas 稀疏数组。

可迭代内省#

api.types.is_dict_like (obj

检查对象是否为 dict-like。

api.types.is_file_like (obj)

检查该对象是否为类文件对象。

api.types.is_list_like (obj[, allow_sets])

检查该对象是否为类列表对象。

api.types.is_named_tuple (obj)

检查该对象是否为命名元组。

api.types.is_iterator (obj)

检查该对象是否为迭代器。

标量自省#

api.types.is_bool (obj)

如果给定对象是布尔类型,则返回 True。

api.types.is_complex (obj)

如果给定对象是复数类型,则返回 True。

api.types.is_float (obj)

如果给定对象是浮点数,则返回 True。

api.types.is_hashable (obj)

如果 hash(obj) 成功,则返回 True,否则返回 False。

api.types.is_integer (obj)

如果给定对象是整数,则返回 True。

api.types.is_interval (obj)

api.types.is_number (obj)

检查该对象是否为数字。

api.types.is_re (obj)

检查该对象是否为正则表达式模式实例。

api.types.is_re_compilable (obj)

检查该对象是否可以编译为正则表达式模式实例。

api.types.is_scalar (val)

如果给定对象为标量,则返回 True。