1.0.0 版本有什么新变化 (2020 年 1 月 29 日)#

以下是 pandas 1.0.0 版本中的变化。有关完整的更改日志,包括其他版本的 pandas,请参阅发行说明

注意

pandas 1.0 版本移除了之前版本中许多已弃用的功能(概览请参见下方)。建议您先升级到 pandas 0.25 并确保您的代码没有警告,然后再升级到 pandas 1.0。

新的弃用策略#

从 pandas 1.0.0 版本开始,pandas 将采用 SemVer 的变体来管理版本发布。简而言之,

  • 弃用将在次要版本中引入(例如 1.1.0, 1.2.0, 2.1.0, …)

  • 弃用将在主要版本中强制执行(例如 1.0.0, 2.0.0, 3.0.0, …)

  • API 破坏性更改仅在主要版本中进行(实验性功能除外)

详情请参见版本策略

增强功能#

rolling.applyexpanding.apply 中使用 Numba#

我们在 apply()apply() 中添加了一个 engine 关键字,允许用户使用 Numba 而不是 Cython 执行例程。如果 apply 函数可以在 numpy 数组上操作并且数据集较大(100 万行或更多),使用 Numba 引擎可以显著提高性能。更多详情请参见滚动应用文档 (GH 28987, GH 30936)

为滚动操作定义自定义窗口#

我们添加了一个 pandas.api.indexers.BaseIndexer() 类,允许用户定义在 rolling 操作期间如何创建窗口边界。用户可以在 pandas.api.indexers.BaseIndexer() 子类上定义自己的 get_window_bounds 方法,该方法将在滚动聚合期间生成每个窗口使用的起始和结束索引。有关更多详细信息和示例用法,请参见自定义窗口滚动文档

转换为 markdown#

我们添加了 to_markdown(),用于创建 markdown 表格 (GH 11052)

In [1]: df = pd.DataFrame({"A": [1, 2, 3], "B": [1, 2, 3]}, index=['a', 'a', 'b'])

In [2]: print(df.to_markdown())
|    |   A |   B |
|:---|----:|----:|
| a  |   1 |   1 |
| a  |   2 |   2 |
| b  |   3 |   3 |

实验性新功能#

实验性 NA 标量表示缺失值#

引入了一个新的 pd.NA 值(单例)来表示标量缺失值。到目前为止,pandas 使用了几个值来表示缺失数据:np.nan 用于浮点数据,np.nanNone 用于对象-dtype 数据,以及 pd.NaT 用于日期时间类数据。pd.NA 的目标是提供一个可在不同数据类型中一致使用的“缺失”指示符。pd.NA 目前由可空整数和布尔数据类型以及新的字符串数据类型使用 (GH 28095)。

警告

实验性功能:pd.NA 的行为仍可能在不发出警告的情况下发生变化。

例如,使用可空整数 dtype 创建一个 Series

In [3]: s = pd.Series([1, 2, None], dtype="Int64")

In [4]: s
Out[4]: 
0       1
1       2
2    <NA>
Length: 3, dtype: Int64

In [5]: s[2]
Out[5]: <NA>

np.nan 相比,pd.NA 在某些操作中的行为不同。除了算术运算外,pd.NA 在比较操作中也会传播为“缺失”或“未知”状态

In [6]: np.nan > 1
Out[6]: False

In [7]: pd.NA > 1
Out[7]: <NA>

对于逻辑运算,pd.NA 遵循三值逻辑(或称 Kleene 逻辑)的规则。例如

In [8]: pd.NA | True
Out[8]: True

更多详情请参见用户指南中关于缺失数据的NA 部分

专用字符串数据类型#

我们添加了 StringDtype,这是一种专门用于字符串数据的扩展类型。以前,字符串通常存储在 object-dtype 的 NumPy 数组中。(GH 29975)

警告

StringDtype 目前被认为是实验性的。其实现和部分 API 可能会在不发出警告的情况下发生变化。

The 'string' 扩展类型解决了 object-dtype NumPy 数组的几个问题

  1. 你可能会意外地在 object dtype 数组中存储字符串和非字符串的混合。而 StringArray 只能存储字符串。

  2. object dtype 会破坏 dtype 特定的操作,例如 DataFrame.select_dtypes()。没有明确的方法可以在排除非文本(但仍然是 object-dtype)列的同时,仅选择文本

  3. 阅读代码时,object dtype 数组的内容不如 string 清晰。

In [9]: pd.Series(['abc', None, 'def'], dtype=pd.StringDtype())
Out[9]: 
0     abc
1    <NA>
2     def
Length: 3, dtype: string

你也可以使用别名 "string"

In [10]: s = pd.Series(['abc', None, 'def'], dtype="string")

In [11]: s
Out[11]: 
0     abc
1    <NA>
2     def
Length: 3, dtype: string

常用的字符串访问器方法(string accessor methods)仍然有效。在适当的情况下,Series 或 DataFrame 列的返回类型也将具有 string dtype。

In [12]: s.str.upper()
Out[12]: 
0     ABC
1    <NA>
2     DEF
Length: 3, dtype: string

In [13]: s.str.split('b', expand=True).dtypes
Out[13]: 
0    string[python]
1    string[python]
Length: 2, dtype: object

返回整数的字符串访问器方法将返回一个具有 Int64Dtype 类型的值

In [14]: s.str.count("a")
Out[14]: 
0       1
1    <NA>
2       0
Length: 3, dtype: Int64

我们建议在处理字符串时明确使用 string 数据类型。更多详情请参见文本数据类型

支持缺失值的布尔数据类型#

我们添加了 BooleanDtype / BooleanArray,这是一种专门用于布尔数据的扩展类型,可以存储缺失值。基于 bool-dtype NumPy 数组的默认 bool 数据类型列只能存储 TrueFalse,不能存储缺失值。这种新的 BooleanArray 通过在一个单独的掩码中跟踪缺失情况,也可以存储缺失值。(GH 29555, GH 30095, GH 31131)

In [15]: pd.Series([True, False, None], dtype=pd.BooleanDtype())
Out[15]: 
0     True
1    False
2     <NA>
Length: 3, dtype: boolean

你也可以使用别名 "boolean"

In [16]: s = pd.Series([True, False, None], dtype="boolean")

In [17]: s
Out[17]: 
0     True
1    False
2     <NA>
Length: 3, dtype: boolean

方法 convert_dtypes 简化支持的扩展 dtype 的使用#

为了鼓励使用支持 pd.NA 的扩展 dtype,例如 StringDtype, BooleanDtype, Int64Dtype, Int32Dtype 等,我们引入了 DataFrame.convert_dtypes()Series.convert_dtypes() 方法。(GH 29752) (GH 30929)

示例

In [18]: df = pd.DataFrame({'x': ['abc', None, 'def'],
   ....:                    'y': [1, 2, np.nan],
   ....:                    'z': [True, False, True]})
   ....: 

In [19]: df
Out[19]: 
      x    y      z
0   abc  1.0   True
1  None  2.0  False
2   def  NaN   True

[3 rows x 3 columns]

In [20]: df.dtypes
Out[20]: 
x     object
y    float64
z       bool
Length: 3, dtype: object
In [21]: converted = df.convert_dtypes()

In [22]: converted
Out[22]: 
      x     y      z
0   abc     1   True
1  <NA>     2  False
2   def  <NA>   True

[3 rows x 3 columns]

In [23]: converted.dtypes
Out[23]: 
x    string[python]
y             Int64
z           boolean
Length: 3, dtype: object

这在使用 read_csv()read_excel() 等读取器读取数据后特别有用。有关描述,请参见此处

其他增强功能#

向后不兼容的 API 变更#

避免使用 MultiIndex.levels 中的名称#

作为 MultiIndex 重大重构的一部分,级别名称现在与级别分开存储 (GH 27242)。我们建议使用 MultiIndex.names 来访问名称,并使用 Index.set_names() 来更新名称。

为了向后兼容,您仍然可以通过级别访问名称。

In [24]: mi = pd.MultiIndex.from_product([[1, 2], ['a', 'b']], names=['x', 'y'])

In [25]: mi.levels[0].name
Out[25]: 'x'

但是,不再可能通过级别更新 MultiIndex 的名称。

In [26]: mi.levels[0].name = "new name"
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[26], line 1
----> 1 mi.levels[0].name = "new name"

File ~/work/pandas/pandas/pandas/core/indexes/base.py:1690, in Index.name(self, value)
   1686 @name.setter
   1687 def name(self, value: Hashable) -> None:
   1688     if self._no_setting_name:
   1689         # Used in MultiIndex.levels to avoid silently ignoring name updates.
-> 1690         raise RuntimeError(
   1691             "Cannot set name on a level of a MultiIndex. Use "
   1692             "'MultiIndex.set_names' instead."
   1693         )
   1694     maybe_extract_name(value, None, type(self))
   1695     self._name = value

RuntimeError: Cannot set name on a level of a MultiIndex. Use 'MultiIndex.set_names' instead.

In [27]: mi.names
Out[27]: FrozenList(['x', 'y'])

要更新,请使用 MultiIndex.set_names,该方法返回一个新的 MultiIndex

In [28]: mi2 = mi.set_names("new name", level=0)

In [29]: mi2.names
Out[29]: FrozenList(['new name', 'y'])

IntervalArray 的新 repr#

pandas.arrays.IntervalArray 采用了一种新的 __repr__,与其他数组类一致 (GH 25022)

pandas 0.25.x

In [1]: pd.arrays.IntervalArray.from_tuples([(0, 1), (2, 3)])
Out[2]:
IntervalArray([(0, 1], (2, 3]],
              closed='right',
              dtype='interval[int64]')

pandas 1.0.0

In [30]: pd.arrays.IntervalArray.from_tuples([(0, 1), (2, 3)])
Out[30]: 
<IntervalArray>
[(0, 1], (2, 3]]
Length: 2, dtype: interval[int64, right]

DataFrame.rename 现在只接受一个位置参数#

DataFrame.rename() 之前会接受可能导致歧义或未定义行为的位置参数。从 pandas 1.0 开始,只有第一个参数,即沿默认轴将标签映射到新名称的参数,允许通过位置传递 (GH 29136)。

pandas 0.25.x

In [1]: df = pd.DataFrame([[1]])
In [2]: df.rename({0: 1}, {0: 2})
Out[2]:
FutureWarning: ...Use named arguments to resolve ambiguity...
   2
1  1

pandas 1.0.0

In [3]: df.rename({0: 1}, {0: 2})
Traceback (most recent call last):
...
TypeError: rename() takes from 1 to 2 positional arguments but 3 were given

请注意,现在提供冲突或可能含糊不清的参数时会引发错误。

pandas 0.25.x

In [4]: df.rename({0: 1}, index={0: 2})
Out[4]:
   0
1  1

In [5]: df.rename(mapper={0: 1}, index={0: 2})
Out[5]:
   0
2  1

pandas 1.0.0

In [6]: df.rename({0: 1}, index={0: 2})
Traceback (most recent call last):
...
TypeError: Cannot specify both 'mapper' and any of 'index' or 'columns'

In [7]: df.rename(mapper={0: 1}, index={0: 2})
Traceback (most recent call last):
...
TypeError: Cannot specify both 'mapper' and any of 'index' or 'columns'

您仍然可以通过提供 axis 关键字参数来更改应用第一个位置参数的轴。

In [31]: df.rename({0: 1})
Out[31]: 
   0
1  1

[1 rows x 1 columns]

In [32]: df.rename({0: 1}, axis=1)
Out[32]: 
   1
0  1

[1 rows x 1 columns]

如果您想同时更新索引和列标签,请务必使用相应的关键字参数。

In [33]: df.rename(index={0: 1}, columns={0: 2})
Out[33]: 
   2
1  1

[1 rows x 1 columns]

DataFrame 扩展了详细信息输出#

DataFrame.info() 现在显示列摘要的行号 (GH 17304)

pandas 0.25.x

In [1]: df = pd.DataFrame({"int_col": [1, 2, 3],
...                    "text_col": ["a", "b", "c"],
...                    "float_col": [0.0, 0.1, 0.2]})
In [2]: df.info(verbose=True)
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
int_col      3 non-null int64
text_col     3 non-null object
float_col    3 non-null float64
dtypes: float64(1), int64(1), object(1)
memory usage: 152.0+ bytes

pandas 1.0.0

In [34]: df = pd.DataFrame({"int_col": [1, 2, 3],
   ....:                    "text_col": ["a", "b", "c"],
   ....:                    "float_col": [0.0, 0.1, 0.2]})
   ....: 

In [35]: df.info(verbose=True)
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
 #   Column     Non-Null Count  Dtype  
---  ------     --------------  -----  
 0   int_col    3 non-null      int64  
 1   text_col   3 non-null      object 
 2   float_col  3 non-null      float64
dtypes: float64(1), int64(1), object(1)
memory usage: 200.0+ bytes

pandas.array() 推断变更#

pandas.array() 现在在几种情况下推断 pandas 的新扩展类型 (GH 29791)

  1. 字符串数据(包括缺失值)现在返回 arrays.StringArray

  2. 整数数据(包括缺失值)现在返回 arrays.IntegerArray

  3. 布尔数据(包括缺失值)现在返回新的 arrays.BooleanArray

pandas 0.25.x

In [1]: pd.array(["a", None])
Out[1]:
<PandasArray>
['a', None]
Length: 2, dtype: object

In [2]: pd.array([1, None])
Out[2]:
<PandasArray>
[1, None]
Length: 2, dtype: object

pandas 1.0.0

In [36]: pd.array(["a", None])
Out[36]: 
<StringArray>
['a', <NA>]
Length: 2, dtype: string

In [37]: pd.array([1, None])
Out[37]: 
<IntegerArray>
[1, <NA>]
Length: 2, dtype: Int64

提醒一下,您可以指定 dtype 来禁用所有推断。

arrays.IntegerArray 现在使用 pandas.NA#

arrays.IntegerArray 现在使用 pandas.NA 而不是 numpy.nan 作为其缺失值标记 (GH 29964)。

pandas 0.25.x

In [1]: a = pd.array([1, 2, None], dtype="Int64")
In [2]: a
Out[2]:
<IntegerArray>
[1, 2, NaN]
Length: 3, dtype: Int64

In [3]: a[2]
Out[3]:
nan

pandas 1.0.0

In [38]: a = pd.array([1, 2, None], dtype="Int64")

In [39]: a
Out[39]: 
<IntegerArray>
[1, 2, <NA>]
Length: 3, dtype: Int64

In [40]: a[2]
Out[40]: <NA>

这带来了一些 API 破坏性后果。

转换为 NumPy ndarray

转换为NumPy数组时,缺失值将是 pd.NA ,它不能转换为浮点数。因此,调用 np.asarray(integer_array, dtype="float") 现在将引发错误。

pandas 0.25.x

In [1]: np.asarray(a, dtype="float")
Out[1]:
array([ 1.,  2., nan])

pandas 1.0.0

In [41]: np.asarray(a, dtype="float")
Out[41]: array([ 1.,  2., nan])

请改用 arrays.IntegerArray.to_numpy() 并明确指定 na_value

In [42]: a.to_numpy(dtype="float", na_value=np.nan)
Out[42]: array([ 1.,  2., nan])

聚合运算可以返回 pd.NA

当执行聚合运算(如求和)并设置 skipna=False 时,如果存在缺失值,结果现在将是 pd.NA 而不是 np.nan (GH 30958)。

pandas 0.25.x

In [1]: pd.Series(a).sum(skipna=False)
Out[1]:
nan

pandas 1.0.0

In [43]: pd.Series(a).sum(skipna=False)
Out[43]: <NA>

value_counts 返回可空整数数据类型

Series.value_counts() 当输入是可空整数数据类型时,现在为结果值返回可空整数数据类型。

pandas 0.25.x

In [1]: pd.Series([2, 1, 1, None], dtype="Int64").value_counts().dtype
Out[1]:
dtype('int64')

pandas 1.0.0

In [44]: pd.Series([2, 1, 1, None], dtype="Int64").value_counts().dtype
Out[44]: Int64Dtype()

有关 pandas.NAnumpy.nan 的区别的更多信息,请参阅 NA semantics

arrays.IntegerArray 比较运算返回 arrays.BooleanArray#

arrays.IntegerArray 执行比较运算时,现在返回 arrays.BooleanArray ,而不是NumPy数组 (GH 29964)。

pandas 0.25.x

In [1]: a = pd.array([1, 2, None], dtype="Int64")
In [2]: a
Out[2]:
<IntegerArray>
[1, 2, NaN]
Length: 3, dtype: Int64

In [3]: a > 1
Out[3]:
array([False,  True, False])

pandas 1.0.0

In [45]: a = pd.array([1, 2, None], dtype="Int64")

In [46]: a > 1
Out[46]: 
<BooleanArray>
[False, True, <NA>]
Length: 3, dtype: boolean

注意,缺失值现在会传播(propagate),而不是像 numpy.nan 那样总是比较不相等。详情请参阅 NA semantics

默认情况下 Categorical.min() 现在返回最小值而不是 np.nan#

Categorical 包含 np.nan 时, Categorical.min() 默认情况下(skipna=True)不再返回 np.nan (GH 25303)

pandas 0.25.x

In [1]: pd.Categorical([1, 2, np.nan], ordered=True).min()
Out[1]: nan

pandas 1.0.0

In [47]: pd.Categorical([1, 2, np.nan], ordered=True).min()
Out[47]: 1

pandas.Series 的默认数据类型 (dtype)#

初始化一个空的 pandas.Series 而不指定 dtype 现在将引发一个 DeprecationWarning (GH 17261)。默认的 dtype 在未来的版本中将从 float64 变为 object ,以便与 DataFrame 以及 Index 的行为保持一致。

pandas 1.0.0

In [1]: pd.Series()
Out[2]:
DeprecationWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
Series([], dtype: float64)

resample 运算的结果 dtype 推断变化#

DataFrame.resample() 聚合运算中,扩展类型的结果 dtype 规则已更改 (GH 31359)。以前,pandas 会尝试将结果转换回原始 dtype,如果不可能则回退到通常的推断规则。现在,只有当结果中的标量值是扩展类型的标量类型的实例时,pandas 才会返回原始 dtype 的结果。

In [48]: df = pd.DataFrame({"A": ['a', 'b']}, dtype='category',
   ....:                   index=pd.date_range('2000', periods=2))
   ....: 

In [49]: df
Out[49]: 
            A
2000-01-01  a
2000-01-02  b

[2 rows x 1 columns]

pandas 0.25.x

In [1]> df.resample("2D").agg(lambda x: 'a').A.dtype
Out[1]:
CategoricalDtype(categories=['a', 'b'], ordered=False)

pandas 1.0.0

In [50]: df.resample("2D").agg(lambda x: 'a').A.dtype
Out[50]: CategoricalDtype(categories=['a', 'b'], ordered=False, categories_dtype=object)

这修复了 resamplegroupby 之间的不一致性。这也修复了一个潜在的错误,即结果的可能会因结果如何被强制转换回原始 dtype 而改变。

pandas 0.25.x

In [1] df.resample("2D").agg(lambda x: 'c')
Out[1]:

     A
0  NaN

pandas 1.0.0

In [51]: df.resample("2D").agg(lambda x: 'c')
Out[51]: 
            A
2000-01-01  c

[1 rows x 1 columns]

Python 的最低版本要求提高#

pandas 1.0.0 支持 Python 3.6.1 及更高版本 (GH 29212)。

依赖项的最低版本要求提高#

一些依赖项的最低支持版本已更新 (GH 29766, GH 29723)。如果已安装,我们现在要求

最低版本

必需

已更改

numpy

1.13.3

X

pytz

2015.4

X

python-dateutil

2.6.1

X

bottleneck

1.2.1

numexpr

2.6.2

pytest (dev)

4.0.2

对于 optional libraries ,一般建议使用最新版本。下表列出了在 pandas 开发过程中当前正在测试的每个库的最低版本。低于最低测试版本的可选库可能仍然有效,但不被视为支持。

最低版本

已更改

beautifulsoup4

4.6.0

fastparquet

0.3.2

X

gcsfs

0.2.2

lxml

3.8.0

matplotlib

2.2.2

numba

0.46.0

X

openpyxl

2.5.7

X

pyarrow

0.13.0

X

pymysql

0.7.1

pytables

3.4.2

s3fs

0.3.0

X

scipy

0.19.0

sqlalchemy

1.1.4

xarray

0.8.2

xlrd

1.1.0

xlsxwriter

0.9.8

xlwt

1.2.0

详情请参阅 Dependencies 以及 Optional dependencies

构建变更#

pandas 已添加了一个 pyproject.toml 文件,并且将不再在上传到 PyPI 的源码发布版中包含 cythonized 文件 (GH 28341, GH 20775)。如果你正在安装一个构建好的发布版 (wheel) 或通过 conda 安装,这应该对你没有任何影响。如果你从源码构建 pandas,你不再需要在调用 pip install pandas 前将 Cython 安装到你的构建环境中。

其他 API 变更#

  • DataFrameGroupBy.transform() 以及 SeriesGroupBy.transform() 现在在遇到无效的操作名称时会引发错误 (GH 27489)

  • pandas.api.types.infer_dtype() 现在将针对整数和 np.nan 的混合情况返回 “integer-na” (GH 27283)

  • MultiIndex.from_arrays() 如果在明确提供 names=None 时,将不再从数组推断名称 (GH 27292)

  • 为了改善 tab 键补全体验,在使用 dir 对 pandas 对象进行自省(introspecting)时,pandas 不会包含大多数已弃用的属性(例如 dir(df))。要查看哪些属性被排除,请查看对象的 _deprecations 属性,例如 pd.DataFrame._deprecations (GH 28805)。

  • unique() 的返回 dtype 现在匹配输入的 dtype。 (GH 27874)

  • options.matplotlib.register_converters 的默认配置值已从 True 更改为 "auto" (GH 18720)。现在,pandas 的自定义格式化程序仅应用于通过 plot() 创建的图。以前,在调用 plot() 之后创建的所有图都会应用 pandas 的格式化程序。详情请参阅 units registration

  • Series.dropna() 已移除了其 **kwargs 参数,取而代之的是一个单独的 how 参数。之前,除了 how 以外的任何内容传递给 **kwargs 都会引发一个 TypeError (GH 29388)

  • 测试 pandas 时,pytest 的新的最低要求版本是 5.0.1 (GH 29664)

  • Series.str.__iter__() 已弃用,并将在未来版本中移除 (GH 28277)。

  • 已将 <NA> 添加到 read_csv() 的默认 NA 值列表中 (GH 30821)

文档改进#

弃用项#

  • Series.item() 以及 Index.item()恢复弃用(undeprecated) (GH 29250)

  • Index.set_value 已弃用。对于给定的索引 idx ,数组 arridx 中的值 idx_val 以及新值 validx.set_value(arr, idx_val, val) 相当于 arr[idx.get_loc(idx_val)] = val ,应改用此方法 (GH 28621)。

  • is_extension_type() 已弃用,应改用 is_extension_array_dtype() (GH 29457)

  • eval() 的关键字参数 “truediv” 已弃用,并将在未来版本中移除 (GH 29812)

  • DateOffset.isAnchored() 以及 DatetOffset.onOffset() 已弃用,并将在未来版本中移除;请改用 DateOffset.is_anchored() 以及 DateOffset.is_on_offset() (GH 30340)

  • pandas.tseries.frequencies.get_offset 已弃用,并将在未来版本中移除;请改用 pandas.tseries.frequencies.to_offset (GH 4205)

  • Categorical.take_nd() 以及 CategoricalIndex.take_nd() 已弃用,请改用 Categorical.take() 以及 CategoricalIndex.take() (GH 27745)

  • Categorical.min() 以及 Categorical.max() 的参数 numeric_only 已弃用,并被 skipna 取代 (GH 25303)

  • 参数 labellreshape() 中已弃用,并将在未来版本中移除 (GH 29742)

  • pandas.core.index 已弃用,并将在未来版本中移除;公共类已在顶层命名空间中可用 (GH 19711)

  • pandas.json_normalize() 现在已在顶层命名空间中公开。将 json_normalize 用作 pandas.io.json.json_normalize 现在已弃用,建议改用 pandas.json_normalize() (GH 27586)。

  • pandas.read_json() 的参数 numpy 已弃用 (GH 28512)。

  • DataFrame.to_stata(), DataFrame.to_feather(), 以及 DataFrame.to_parquet() 的参数 “fname” 已弃用,请改用 “path” (GH 23574)

  • RangeIndex 的已弃用内部属性 _start, _stop 以及 _step 现在会引发 FutureWarning ,而不是 DeprecationWarning (GH 26581)

  • pandas.util.testing 模块已弃用。请改用 pandas.testing 中记录的公共 API (Assertion functions) (GH 16232)。

  • pandas.SparseArray 已弃用。请改用 pandas.arrays.SparseArray (arrays.SparseArray)。 (GH 30642)

  • Series.take() 以及 DataFrame.take() 的参数 is_copy 已弃用,并将在未来版本中移除。 (GH 27357)

  • Index 进行多维索引(例如 index[:, None])的支持已弃用,并将在未来版本中移除;请改在索引前将其转换为 numpy 数组 (GH 30588)

  • pandas.np 子模块现在已弃用。请改直接导入 numpy (GH 30296)

  • pandas.datetime 类现在已弃用。请改从 datetime 导入 (GH 30610)

  • diff 未来将引发 TypeError ,而不是隐式地丢失扩展类型的 dtype。请改在调用 diff 前将其转换为正确的 dtype (GH 31025)

从分组 DataFrame 中选择列

DataFrameGroupBy 对象中选择列时,使用单方括号传递单个键(或键的元组)已弃用,应改用列表。 (GH 23566) 例如

df = pd.DataFrame({
    "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"],
    "B": np.random.randn(8),
    "C": np.random.randn(8),
})
g = df.groupby('A')

# single key, returns SeriesGroupBy
g['B']

# tuple of single key, returns SeriesGroupBy
g[('B',)]

# tuple of multiple keys, returns DataFrameGroupBy, raises FutureWarning
g[('B', 'C')]

# multiple keys passed directly, returns DataFrameGroupBy, raises FutureWarning
# (implicitly converts the passed strings into a single tuple)
g['B', 'C']

# proper way, returns DataFrameGroupBy
g[['B', 'C']]

移除先前版本的弃用项/变更#

已移除 SparseSeries 以及 SparseDataFrame

SparseSeries, SparseDataFrame 以及 DataFrame.to_sparse 方法已移除 (GH 28425)。我们建议改用具有稀疏值的 SeriesDataFrame

Matplotlib 单元注册

以前,pandas 在导入时会作为副作用向 matplotlib 注册转换器 (GH 18720)。这改变了在导入 pandas 后通过 matplotlib plots 生成的图的输出,即使你直接使用 matplotlib 而不是 plot()

要在 matplotlib 图中使用 pandas 格式化程序,请指定

In [1]: import pandas as pd
In [2]: pd.options.plotting.matplotlib.register_converters = True

请注意,由 DataFrame.plot() 以及 Series.plot() 创建的图会自动注册转换器。唯一的变化是当通过 matplotlib.pyplot.plotmatplotlib.Axes.plot 绘制日期类对象时的行为。详情请参阅 Custom formatters for timeseries plots

其他移除项

性能改进#

错误修复#

分类数据 (Categorical)#

日期/时间类型 (Datetimelike)#

  • Series.__setitem__() 中,将 np.timedelta64("NaT") 错误地转换为 np.datetime64("NaT"),当将其插入 datetime64 数据类型的 Series 时,存在错误 (GH 27311)

  • Series.dt() 属性查找中,当底层数据为只读时存在错误 (GH 27529)

  • HDFStore.__getitem__ 中读取 Python 2 中创建的 tz 属性不正确,存在错误 (GH 26443)

  • to_datetime() 中,当传入格式错误的 str 数组且 errors=”coerce” 时,可能错误地引发 ValueError,存在错误 (GH 28299)

  • core.groupby.SeriesGroupBy.nunique() 中,NaT 值会干扰唯一值的计数,存在错误 (GH 27951)

  • Timestamp 减法中,当从 np.datetime64 对象中减去一个 Timestamp 时,错误地引发 TypeError,存在错误 (GH 28286)

  • Timestamp 与整数或整数数据类型数组相加或相减现在将引发 NullFrequencyError 而不是 ValueError (GH 28268)

  • SeriesDataFrame 中,整数 dtype 在添加或减去 np.datetime64 对象时未能引发 TypeError 的错误 (GH 28080)

  • Series.astype()Index.astype()DataFrame.astype() 中,当转换为整数数据类型时未能处理 NaT,存在错误 (GH 28492)

  • 在具有 weekdayWeek 中,当添加或减去无效类型时,错误地引发 AttributeError 而不是 TypeError,存在错误 (GH 28530)

  • DataFrame 算术运算中,当与数据类型为 'timedelta64[ns]'Series 进行操作时存在错误 (GH 28049)

  • core.groupby.generic.SeriesGroupBy.apply() 中,当原始 DataFrame 中的列是 datetime 且列标签不是标准整数时,引发 ValueError,存在错误 (GH 28247)

  • pandas._config.localization.get_locales() 中,locales -a 将区域设置列表编码为 windows-1252,存在错误 (GH 23638, GH 24760, GH 27368)

  • Series.var() 中,当使用 timedelta64[ns] 数据类型调用时未能引发 TypeError,存在错误 (GH 28289)

  • DatetimeIndex.strftime()Series.dt.strftime() 中,NaT 被转换为字符串 'NaT' 而不是 np.nan,存在错误 (GH 29578)

  • 使用长度不正确的布尔掩码来掩码 datetime-like 数组时未能引发 IndexError 的错误 (GH 30308)

  • Timestamp.resolution 中,其是一个属性而不是类属性,存在错误 (GH 29910)

  • pandas.to_datetime() 中,当使用 None 调用时引发 TypeError 而不是返回 NaT,存在错误 (GH 30011)

  • pandas.to_datetime() 中,当使用 cache=True (默认值) 时,对 deque 对象会失败,存在错误 (GH 29403)

  • 在数据类型为 datetime64timedelta64Series.item()DatetimeIndex.item()TimedeltaIndex.item() 中,返回的是整数而不是 TimestampTimedelta,存在错误 (GH 30175)

  • DatetimeIndex 加法中,当添加非优化的 DateOffset 时,错误地丢弃时区信息,存在错误 (GH 30336)

  • DataFrame.drop() 中,尝试从 DatetimeIndex 中删除不存在的值时会产生令人困惑的错误消息,存在错误 (GH 30399)

  • DataFrame.append() 中,会移除新数据的时区感知性,存在错误 (GH 30238)

  • Series.cummin()Series.cummax() 中,对于时区感知的数据类型,错误地丢弃其时区,存在错误 (GH 15553)

  • DatetimeArrayTimedeltaArrayPeriodArray 中,原地加法和减法实际上并未在原地操作,存在错误 (GH 24115)

  • pandas.to_datetime() 中,当使用存储 IntegerArraySeries 调用时引发 TypeError 而不是返回 Series,存在错误 (GH 30050)

  • date_range() 中,当使用自定义工作时间作为 freq 并给定 periods 数量时存在错误 (GH 30593)

  • PeriodIndex 比较中,错误地将整数转换为 Period 对象,与 Period 的比较行为不一致,存在错误 (GH 30722)

  • DatetimeIndex.insert() 中,尝试将时区感知 Timestamp 插入到时区不感知 DatetimeIndex 中,或反之,会引发 ValueError 而不是 TypeError,存在错误 (GH 30806)

时间差 (Timedelta)#

时区 (Timezones)#

数值 (Numeric)#

转换#

字符串#

区间#

索引#

  • 使用反向切片器进行赋值时的错误 (GH 26939)

  • DataFrame.explode() 中,当索引存在重复时会复制 DataFrame 的错误 (GH 28010)

  • 使用包含 Period 的另一种类型的索引重新索引 PeriodIndex() 时的错误 (GH 28323) (GH 28337)

  • 修复使用 .loc 对 numpy 非纳秒 (non-ns) datetime 类型列进行赋值的问题 (GH 27395)

  • Float64Index.astype() 中,将 np.inf 转换为整数 dtype 时未能正确处理的错误 (GH 28475)

  • 当左侧包含重复项时,Index.union() 可能会失败 (GH 28257)

  • 使用 .loc 索引时,当索引是包含非字符串类别的 CategoricalIndex 时不起作用的错误 (GH 17569, GH 30225)

  • 在某些情况下,例如在字符串索引中搜索整数时,Index.get_indexer_non_unique() 可能会因 TypeError 失败 (GH 28257)

  • Float64Index.get_loc() 错误地引发 TypeError 而非 KeyError 的错误 (GH 29189)

  • 在 1 行 DataFrame 中设置 Categorical 值时,DataFrame.loc() 使用了不正确的 dtype 的错误 (GH 25495)

  • 当输入包含缺失值时,MultiIndex.get_loc() 找不到缺失值 (GH 19132)

  • Series.__setitem__() 中,当新数据的长度与 True 值的数量匹配且新数据不是 Seriesnp.array 时,使用布尔索引器错误赋值的错误 (GH 30567)

  • 使用 PeriodIndex 索引时错误地接受表示年份的整数的错误,应使用例如 ser.loc["2007"] 而非 ser.loc[2007] (GH 30763)

缺失值#

多级索引#

  • 如果 verify_integrity 参数为 True (默认值),MultiIndex 的构造函数会验证给定的 sortorder 与实际的 lexsort_depth 是否兼容 (GH 28735)

  • 当标签不在给定级别中时,带有 MultiIndex 的 Series 和 MultiIndex .drop 会引发异常 (GH 8594)

IO#

绘图#

GroupBy/重采样/rolling#

重塑#

稀疏#

  • Bug in SparseDataFrame arithmetic operations incorrectly casting inputs to float (GH 28107)

  • Bug in DataFrame.sparse returning a Series when there was a column named sparse rather than the accessor (GH 30758)

  • Fixed operator.xor() with a boolean-dtype SparseArray. Now returns a sparse result, rather than object dtype (GH 31025)

扩展数组#

  • Bug in arrays.PandasArray when setting a scalar string (GH 28118, GH 28150)。

  • Bug where nullable integers could not be compared to strings (GH 28930)

  • Bug where DataFrame constructor raised ValueError with list-like data and dtype specified (GH 30280)

其他#

  • Trying to set the display.precision, display.max_rows or display.max_columns using set_option() to anything but a None or a positive int will raise a ValueError (GH 23348)

  • Using DataFrame.replace() with overlapping keys in a nested dictionary will no longer raise, now matching the behavior of a flat dictionary (GH 27660)

  • DataFrame.to_csv() and Series.to_csv() now support dicts as compression argument with key 'method' being the compression method and others as additional compression options when the compression method is 'zip'. (GH 26023)

  • Bug in Series.diff() where a boolean series would incorrectly raise a TypeError (GH 17294)

  • Series.append() will no longer raise a TypeError when passed a tuple of Series (GH 28410)

  • Fix corrupted error message when calling pandas.libs._json.encode() on a 0d array (GH 18878)

  • Backtick quoting in DataFrame.query() and DataFrame.eval() can now also be used to use invalid identifiers like names that start with a digit, are python keywords, or are using single character operators. (GH 27017)

  • Bug in pd.core.util.hashing.hash_pandas_object where arrays containing tuples were incorrectly treated as non-hashable (GH 28969)

  • Bug in DataFrame.append() that raised IndexError when appending with empty list (GH 28769)

  • Fix AbstractHolidayCalendar to return correct results for years after 2030 (now goes up to 2200) (GH 27790)

  • Fixed IntegerArray returning inf rather than NaN for operations dividing by 0 (GH 27398)

  • Fixed pow operations for IntegerArray when the other value is 0 or 1 (GH 29997)

  • Bug in Series.count() raises if use_inf_as_na is enabled (GH 29478)

  • Bug in Index where a non-hashable name could be set without raising TypeError (GH 29069)

  • Bug in DataFrame constructor when passing a 2D ndarray and an extension dtype (GH 12513)

  • Bug in DataFrame.to_csv() when supplied a series with a dtype="string" and a na_rep, the na_rep was being truncated to 2 characters. (GH 29975)

  • Bug where DataFrame.itertuples() would incorrectly determine whether or not namedtuples could be used for dataframes of 255 columns (GH 28282)

  • Handle nested NumPy object arrays in testing.assert_series_equal() for ExtensionArray implementations (GH 30841)

  • Bug in Index constructor incorrectly allowing 2-dimensional input arrays (GH 13601, GH 27125)

贡献者#

共有 308 人为此版本贡献了补丁。姓名旁边带有“+”的人员是首次贡献补丁。

  • Aaditya Panikath +

  • Abdullah İhsan Seçer

  • Abhijeet Krishnan +

  • Adam J. Stewart

  • Adam Klaum +

  • Addison Lynch

  • Aivengoe +

  • Alastair James +

  • Albert Villanova del Moral

  • Alex Kirko +

  • Alfredo Granja +

  • Allen Downey

  • Alp Arıbal +

  • Andreas Buhr +

  • Andrew Munch +

  • Andy

  • Angela Ambroz +

  • Aniruddha Bhattacharjee +

  • Ankit Dhankhar +

  • Antonio Andraues Jr +

  • Arda Kosar +

  • Asish Mahapatra +

  • Austin Hackett +

  • Avi Kelman +

  • AyowoleT +

  • Bas Nijholt +

  • Ben Thayer

  • Bharat Raghunathan

  • Bhavani Ravi

  • Bhuvana KA +

  • Big Head

  • Blake Hawkins +

  • Bobae Kim +

  • Brett Naul

  • Brian Wignall

  • Bruno P. Kinoshita +

  • Bryant Moscon +

  • Cesar H +

  • Chris Stadler

  • Chris Zimmerman +

  • Christopher Whelan

  • Clemens Brunner

  • Clemens Tolboom +

  • Connor Charles +

  • Daniel Hähnke +

  • Daniel Saxton

  • Darin Plutchok +

  • Dave Hughes

  • David Stansby

  • DavidRosen +

  • Dean +

  • Deepan Das +

  • Deepyaman Datta

  • DorAmram +

  • Dorothy Kabarozi +

  • Drew Heenan +

  • Eliza Mae Saret +

  • Elle +

  • Endre Mark Borza +

  • Eric Brassell +

  • Eric Wong +

  • Eunseop Jeong +

  • Eyden Villanueva +

  • Felix Divo

  • ForTimeBeing +

  • Francesco Truzzi +

  • Gabriel Corona +

  • Gabriel Monteiro +

  • Galuh Sahid +

  • Georgi Baychev +

  • Gina

  • GiuPassarelli +

  • Grigorios Giannakopoulos +

  • Guilherme Leite +

  • Guilherme Salomé +

  • Gyeongjae Choi +

  • Harshavardhan Bachina +

  • Harutaka Kawamura +

  • Hassan Kibirige

  • Hielke Walinga

  • Hubert

  • Hugh Kelley +

  • Ian Eaves +

  • Ignacio Santolin +

  • Igor Filippov +

  • Irv Lustig

  • Isaac Virshup +

  • Ivan Bessarabov +

  • JMBurley +

  • Jack Bicknell +

  • Jacob Buckheit +

  • Jan Koch

  • Jan Pipek +

  • Jan Škoda +

  • Jan-Philip Gehrcke

  • Jasper J.F. van den Bosch +

  • Javad +

  • Jeff Reback

  • Jeremy Schendel

  • Jeroen Kant +

  • Jesse Pardue +

  • Jethro Cao +

  • Jiang Yue

  • Jiaxiang +

  • Jihyung Moon +

  • Jimmy Callin

  • Jinyang Zhou +

  • Joao Victor Martinelli +

  • Joaq Almirante +

  • John G Evans +

  • John Ward +

  • Jonathan Larkin +

  • Joris Van den Bossche

  • Josh Dimarsky +

  • Joshua Smith +

  • Josiah Baker +

  • Julia Signell +

  • Jung Dong Ho +

  • Justin Cole +

  • Justin Zheng

  • Kaiqi Dong

  • Karthigeyan +

  • Katherine Younglove +

  • Katrin Leinweber

  • Kee Chong Tan +

  • Keith Kraus +

  • Kevin Nguyen +

  • Kevin Sheppard

  • Kisekka David +

  • Koushik +

  • Kyle Boone +

  • Kyle McCahill +

  • Laura Collard, PhD +

  • LiuSeeker +

  • Louis Huynh +

  • Lucas Scarlato Astur +

  • Luiz Gustavo +

  • Luke +

  • Luke Shepard +

  • MKhalusova +

  • Mabel Villalba

  • Maciej J +

  • Mak Sze Chun

  • Manu NALEPA +

  • Marc

  • Marc Garcia

  • Marco Gorelli +

  • Marco Neumann +

  • Martin Winkel +

  • Martina G. Vilas +

  • Mateusz +

  • Matthew Roeschke

  • Matthew Tan +

  • Max Bolingbroke

  • Max Chen +

  • MeeseeksMachine

  • Miguel +

  • MinGyo Jung +

  • Mohamed Amine ZGHAL +

  • Mohit Anand +

  • MomIsBestFriend +

  • Naomi Bonnin +

  • Nathan Abel +

  • Nico Cernek +

  • Nigel Markey +

  • Noritada Kobayashi +

  • Oktay Sabak +

  • Oliver Hofkens +

  • Oluokun Adedayo +

  • Osman +

  • Oğuzhan Öğreden +

  • Pandas Development Team +

  • Patrik Hlobil +

  • Paul Lee +

  • Paul Siegel +

  • Petr Baev +

  • Pietro Battiston

  • Prakhar Pandey +

  • Puneeth K +

  • Raghav +

  • Rajat +

  • Rajhans Jadhao +

  • Rajiv Bharadwaj +

  • Rik-de-Kort +

  • Roei.r

  • Rohit Sanjay +

  • Ronan Lamy +

  • Roshni +

  • Roymprog +

  • Rushabh Vasani +

  • Ryan Grout +

  • Ryan Nazareth

  • Samesh Lakhotia +

  • Samuel Sinayoko

  • Samyak Jain +

  • Sarah Donehower +

  • Sarah Masud +

  • Saul Shanabrook +

  • Scott Cole +

  • SdgJlbl +

  • Seb +

  • Sergei Ivko +

  • Shadi Akiki

  • Shorokhov Sergey

  • Siddhesh Poyarekar +

  • Sidharthan Nair +

  • Simon Gibbons

  • Simon Hawkins

  • Simon-Martin Schröder +

  • Sofiane Mahiou +

  • Sourav kumar +

  • Souvik Mandal +

  • Soyoun Kim +

  • Sparkle Russell-Puleri +

  • Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)

  • Stuart Berg +

  • Sumanau Sareen

  • Szymon Bednarek +

  • Tambe Tabitha Achere +

  • Tan Tran

  • Tang Heyi +

  • Tanmay Daripa +

  • Tanya Jain

  • Terji Petersen

  • Thomas Li +

  • Tirth Jain +

  • Tola A +

  • Tom Augspurger

  • Tommy Lynch +

  • Tomoyuki Suzuki +

  • Tony Lorenzo

  • Unprocessable +

  • Uwe L. Korn

  • Vaibhav Vishal

  • Victoria Zdanovskaya +

  • Vijayant +

  • Vishwak Srinivasan +

  • WANG Aiyong

  • Wenhuan

  • Wes McKinney

  • Will Ayd

  • Will Holmgren

  • William Ayd

  • William Blan +

  • Wouter Overmeire

  • Wuraola Oyewusi +

  • YaOzI +

  • Yash Shukla +

  • Yu Wang +

  • Yusei Tahara +

  • alexander135 +

  • alimcmaster1

  • avelineg +

  • bganglia +

  • bolkedebruin

  • bravech +

  • chinhwee +

  • cruzzoe +

  • dalgarno +

  • daniellebrown +

  • danielplawrence

  • est271 +

  • francisco souza +

  • ganevgv +

  • garanews +

  • gfyoung

  • h-vetinari

  • hasnain2808 +

  • ianzur +

  • jalbritt +

  • jbrockmendel

  • jeschwar +

  • jlamborn324 +

  • joy-rosie +

  • kernc

  • killerontherun1

  • krey +

  • lexy-lixinyu +

  • lucyleeow +

  • lukasbk +

  • maheshbapatu +

  • mck619 +

  • nathalier

  • naveenkaushik2504 +

  • nlepleux +

  • nrebena

  • ohad83 +

  • pilkibun

  • pqzx +

  • proost +

  • pv8493013j +

  • qudade +

  • rhstanton +

  • rmunjal29 +

  • sangarshanan +

  • sardonick +

  • saskakarsi +

  • shaido987 +

  • ssikdar1

  • steveayers124 +

  • tadashigaki +

  • timcera +

  • tlaytongoogle +

  • tobycheese

  • tonywu1999 +

  • tsvikas +

  • yogendrasoni +

  • zys5945 +