1.0.0 (2020年1月29日) 有何新变化#

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

注意

pandas 1.0 版本移除了许多在之前版本中已弃用的功能(有关概述,请参阅下文)。建议在升级到 pandas 1.0 之前,先升级到 pandas 0.25 并确保您的代码没有警告。

新弃用策略#

从 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,这是一种专用于字符串数据的扩展类型。以前,字符串通常存储在对象-dtype NumPy 数组中。 (GH 29975)

警告

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

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

  1. 您可能会不小心在 object dtype 数组中存储字符串和非字符串的混合。而 StringArray 只能存储字符串。

  2. object dtype 会破坏特定于 dtype 的操作,例如 DataFrame.select_dtypes()。没有明确的方法可以选择文本,同时排除非文本但仍是对象-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

常用的字符串访问器方法有效。在适当的情况下,Series 或 DataFrame 列的返回类型也将是字符串 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 数组,该列只能保存 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,例如 StringDtypeBooleanDtypeInt64DtypeInt32Dtype 等,我们引入了 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:1697, in Index.name(self, value)
   1693 @name.setter
   1694 def name(self, value: Hashable) -> None:
   1695     if self._no_setting_name:
   1696         # Used in MultiIndex.levels to avoid silently ignoring name updates.
-> 1697         raise RuntimeError(
   1698             "Cannot set name on a level of a MultiIndex. Use "
   1699             "'MultiIndex.set_names' instead."
   1700         )
   1701     maybe_extract_name(value, None, type(self))
   1702     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 的新表示#

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])

请改用带有显式 na_valuearrays.IntegerArray.to_numpy()

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 返回一个可空整数 dtype

带有可空整数 dtype 的 Series.value_counts() 现在为这些值返回一个可空整数 dtype。

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 语义

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

请注意,缺失值现在会传播,而不是像 numpy.nan 那样始终不相等。更多信息请参阅NA 语义

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

Categorical 包含 np.nan 时,Categorical.min() 默认不再返回 np.nan (skipna=True) (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#

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

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)

重新采样操作的结果 dtype 推断更改#

DataFrame.resample() 聚合中结果 dtype 的规则已针对扩展类型发生变化 (GH 31359)。以前,pandas 会尝试将结果转换回原始 dtype,如果不可能则回退到通常的推断规则。现在,只有当结果中的标量值是扩展 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 (开发)

4.0.2

对于可选库,一般建议使用最新版本。下表列出了在 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

更多信息请参阅依赖项可选依赖项

构建更改#

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)

  • 如果显式提供了 names=NoneMultiIndex.from_arrays() 将不再从数组中推断名称 (GH 27292)

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

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

  • options.matplotlib.register_converters 的默认配置值从 True 更改为 "auto" (GH 18720)。现在,pandas 自定义格式化程序将仅通过 plot() 应用于由 pandas 创建的绘图。以前,pandas 的格式化程序会应用于在 plot() 之后创建的所有绘图。更多信息请参阅单位注册

  • Series.dropna() 已弃用其 **kwargs 参数,转而使用单个 how 参数。以前,向 **kwargs 提供 how 以外的任何内容都会引发 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() 已被_取消弃用_ (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)

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

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

  • pandas.json_normalize() 现在已在顶级命名空间中公开。现在已弃用将 json_normalize 作为 pandas.io.json.json_normalize 使用,建议改用 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 模块已弃用。请使用 Assertion functions 中记录的 pandas.testing 中的公共 API (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

SparseSeriesSparseDataFrameDataFrame.to_sparse 方法已移除 (GH 28425)。我们建议改用包含稀疏值的 SeriesDataFrame

Matplotlib 单位注册

以前,pandas 会在导入时附带地向 matplotlib 注册转换器 (GH 18720)。这改变了在导入 pandas 后通过 matplotlib 绘图生成的输出,即使您是直接使用 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 绘制日期类对象时。更多信息请参阅时间序列绘图的自定义格式化程序

其他移除

性能改进#

Bug 修复#

Categorical#

日期时间类#

  • Series.__setitem__() 中的 Bug,在插入到 datetime64 dtype 的 Series 中时,错误地将 np.timedelta64("NaT") 转换为 np.datetime64("NaT") (GH 27311)

  • 当基础数据是只读时,Series.dt() 属性查找中的 Bug (GH 27529)

  • HDFStore.__getitem__ 中错误读取 Python 2 中创建的 tz 属性的 Bug (GH 26443)

  • to_datetime() 中的 Bug,其中传递格式错误的 str 数组并使用 errors="coerce" 时,可能会错误地导致引发 ValueError (GH 28299)

  • core.groupby.SeriesGroupBy.nunique() 中的 Bug,其中 NaT 值干扰了唯一值的计数 (GH 27951)

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

  • Timestamp 与整数或整数 dtype 数组的加减运算现在将引发 NullFrequencyError 而不是 ValueError (GH 28268)

  • 具有整数 dtype 的 SeriesDataFrame 在添加或减去 np.datetime64 对象时未能引发 TypeError 的 Bug (GH 28080)

  • Series.astype(), Index.astype()DataFrame.astype() 在强制转换为整数 dtype 时未能处理 NaT 的 Bug (GH 28492)

  • Week 带有 weekday 时,在添加或减去无效类型时错误地引发 AttributeError 而不是 TypeError 的 Bug (GH 28530)

  • 当与 dtype 为 'timedelta64[ns]'Series 进行运算时,DataFrame 算术运算中的 Bug (GH 28049)

  • core.groupby.generic.SeriesGroupBy.apply() 中的 Bug,当原始 DataFrame 中的列是日期时间且列标签不是标准整数时,引发 ValueError (GH 28247)

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

  • Series.var() 在使用 timedelta64[ns] dtype 调用时未能引发 TypeError 的 Bug (GH 28289)

  • DatetimeIndex.strftime()Series.dt.strftime() 中的 Bug,其中 NaT 被转换为字符串 'NaT' 而不是 np.nan (GH 29578)

  • 使用长度不正确的布尔掩码对日期时间类数组进行掩码操作时未引发 IndexError 的 Bug (GH 30308)

  • Timestamp.resolution 是一个属性而不是类属性的 Bug (GH 29910)

  • pandas.to_datetime() 在调用时传入 None 却引发 TypeError 而不是返回 NaT 的 Bug (GH 30011)

  • pandas.to_datetime() 在使用 cache=True(默认值)时对 deque 对象失败的 Bug (GH 29403)

  • Series.item()datetime64timedelta64 数据类型、DatetimeIndex.item()TimedeltaIndex.item() 中存在错误,它们返回整数而不是 TimestampTimedeltaGH 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#

时区#

数值#

Conversion#

Strings#

Interval#

Indexing#

  • Bug in assignment using a reverse slicer (GH 26939)

  • Bug in DataFrame.explode() would duplicate frame in the presence of duplicates in the index (GH 28010)

  • Bug in reindexing a PeriodIndex() with another type of index that contained a Period (GH 28323) (GH 28337)

  • Fix assignment of column via .loc with numpy non-ns datetime type (GH 27395)

  • Bug in Float64Index.astype() where np.inf was not handled properly when casting to an integer dtype (GH 28475)

  • Index.union() could fail when the left contained duplicates (GH 28257)

  • Bug when indexing with .loc where the index was a CategoricalIndex with non-string categories didn’t work (GH 17569, GH 30225)

  • Index.get_indexer_non_unique() could fail with TypeError in some cases, such as when searching for ints in a string index (GH 28257)

  • Bug in Float64Index.get_loc() incorrectly raising TypeError instead of KeyError (GH 29189)

  • Bug in DataFrame.loc() with incorrect dtype when setting Categorical value in 1-row DataFrame (GH 25495)

  • MultiIndex.get_loc() can’t find missing values when input includes missing values (GH 19132)

  • Bug in Series.__setitem__() incorrectly assigning values with boolean indexer when the length of new data matches the number of True values and new data is not a Series or an np.array (GH 30567)

  • Bug in indexing with a PeriodIndex incorrectly accepting integers representing years, use e.g. ser.loc["2007"] instead of ser.loc[2007] (GH 30763)

Missing#

MultiIndex#

  • Constructor for MultiIndex verifies that the given sortorder is compatible with the actual lexsort_depth if verify_integrity parameter is True (the default) (GH 28735)

  • Series and MultiIndex .drop with MultiIndex raise exception if labels not in given in level (GH 8594)

IO#

  • read_csv() now accepts binary mode file buffers when using the Python csv engine (GH 23779)

  • Bug in DataFrame.to_json() where using a Tuple as a column or index value and using orient="columns" or orient="index" would produce invalid JSON (GH 20500)

  • Improve infinity parsing. read_csv() now interprets Infinity, +Infinity, -Infinity as floating point values (GH 10065)

  • Bug in DataFrame.to_csv() where values were truncated when the length of na_rep was shorter than the text input data. (GH 25099)

  • Bug in DataFrame.to_string() where values were truncated using display options instead of outputting the full content (GH 9784)

  • Bug in DataFrame.to_json() where a datetime column label would not be written out in ISO format with orient="table" (GH 28130)

  • Bug in DataFrame.to_parquet() where writing to GCS would fail with engine='fastparquet' if the file did not already exist (GH 28326)

  • Bug in read_hdf() closing stores that it didn’t open when Exceptions are raised (GH 28699)

  • Bug in DataFrame.read_json() where using orient="index" would not maintain the order (GH 28557)

  • Bug in DataFrame.to_html() where the length of the formatters argument was not verified (GH 28469)

  • Bug in DataFrame.read_excel() with engine='ods' when sheet_name argument references a non-existent sheet (GH 27676)

  • Bug in pandas.io.formats.style.Styler() formatting for floating values not displaying decimals correctly (GH 13257)

  • Bug in DataFrame.to_html() when using formatters=<list> and max_cols together. (GH 25955)

  • Bug in Styler.background_gradient() not able to work with dtype Int64 (GH 28869)

  • Bug in DataFrame.to_clipboard() which did not work reliably in ipython (GH 22707)

  • Bug in read_json() where default encoding was not set to utf-8 (GH 29565)

  • Bug in PythonParser where str and bytes were being mixed when dealing with the decimal field (GH 29650)

  • read_gbq() now accepts progress_bar_type to display progress bar while the data downloads. (GH 29857)

  • Bug in pandas.io.json.json_normalize() where a missing value in the location specified by record_path would raise a TypeError (GH 30148)

  • read_excel() now accepts binary data (GH 15914)

  • Bug in read_csv() in which encoding handling was limited to just the string utf-16 for the C engine (GH 24130)

Plotting#

GroupBy/resample/rolling#

Reshaping#

Sparse#

  • 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)

ExtensionArray#

  • 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)

Other#

  • 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)

Contributors#

A total of 308 people contributed patches to this release. People with a “+” by their names contributed a patch for the first time.

  • 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 +