0.23.0 版本新特性 (2018 年 5 月 15 日)#
这是继 0.22.0 之后的一个主要版本,包含了多项 API 变更、弃用、新功能、增强功能和性能改进,以及大量的 bug 修复。我们建议所有用户升级到此版本。
主要亮点包括
警告
从 2019 年 1 月 1 日起,pandas 的功能发布版本将只支持 Python 3。详情请参阅 取消支持 Python 2.7。
v0.23.0 版本新特性
新特性#
JSON 读/写:使用 orient='table'
支持往返#
现在,DataFrame
可以通过使用 orient='table'
参数写入 JSON 并随后读取回来,同时保留元数据(参见 GH 18912 和 GH 9146)。此前,所有可用的 orient
值都无法保证保留 dtypes 和索引名等元数据。
In [1]: df = pd.DataFrame({'foo': [1, 2, 3, 4],
...: 'bar': ['a', 'b', 'c', 'd'],
...: 'baz': pd.date_range('2018-01-01', freq='d', periods=4),
...: 'qux': pd.Categorical(['a', 'b', 'c', 'c'])},
...: index=pd.Index(range(4), name='idx'))
...:
In [2]: df
Out[2]:
foo bar baz qux
idx
0 1 a 2018-01-01 a
1 2 b 2018-01-02 b
2 3 c 2018-01-03 c
3 4 d 2018-01-04 c
[4 rows x 4 columns]
In [3]: df.dtypes
Out[3]:
foo int64
bar object
baz datetime64[ns]
qux category
Length: 4, dtype: object
In [4]: df.to_json('test.json', orient='table')
In [5]: new_df = pd.read_json('test.json', orient='table')
In [6]: new_df
Out[6]:
foo bar baz qux
idx
0 1 a 2018-01-01 a
1 2 b 2018-01-02 b
2 3 c 2018-01-03 c
3 4 d 2018-01-04 c
[4 rows x 4 columns]
In [7]: new_df.dtypes
Out[7]:
foo int64
bar object
baz datetime64[ns]
qux category
Length: 4, dtype: object
请注意,该往返格式不支持字符串 index
,因为 write_json
默认使用它来表示缺失的索引名。
In [8]: df.index.name = 'index'
In [9]: df.to_json('test.json', orient='table')
In [10]: new_df = pd.read_json('test.json', orient='table')
In [11]: new_df
Out[11]:
foo bar baz qux
0 1 a 2018-01-01 a
1 2 b 2018-01-02 b
2 3 c 2018-01-03 c
3 4 d 2018-01-04 c
[4 rows x 4 columns]
In [12]: new_df.dtypes
Out[12]:
foo int64
bar object
baz datetime64[ns]
qux category
Length: 4, dtype: object
方法 .assign()
接受依赖参数#
DataFrame.assign()
现在对于 Python 3.6 及更高版本接受依赖的关键字参数(另请参阅 PEP 468)。如果参数是可调用对象,后面的关键字参数现在可以引用前面的参数。请参阅此处文档(GH 14207)。
In [13]: df = pd.DataFrame({'A': [1, 2, 3]})
In [14]: df
Out[14]:
A
0 1
1 2
2 3
[3 rows x 1 columns]
In [15]: df.assign(B=df.A, C=lambda x: x['A'] + x['B'])
Out[15]:
A B C
0 1 1 2
1 2 2 4
2 3 3 6
[3 rows x 3 columns]
警告
当您使用 .assign()
更新现有列时,这可能会微妙地改变代码的行为。以前,引用其他正在更新的变量的可调用对象会获取“旧”值。
以前的行为
In [2]: df = pd.DataFrame({"A": [1, 2, 3]})
In [3]: df.assign(A=lambda df: df.A + 1, C=lambda df: df.A * -1)
Out[3]:
A C
0 2 -1
1 3 -2
2 4 -3
新的行为
In [16]: df.assign(A=df.A + 1, C=lambda df: df.A * -1)
Out[16]:
A C
0 2 -2
1 3 -3
2 4 -4
[3 rows x 2 columns]
支持基于列和索引级别的组合进行合并#
传递给 DataFrame.merge()
作为 on
、left_on
和 right_on
参数的字符串现在可以引用列名或索引级别名。这使得可以在不重置索引的情况下,基于索引级别和列的组合合并 DataFrame
实例。请参阅基于列和级别的合并文档部分。(GH 14355)
In [17]: left_index = pd.Index(['K0', 'K0', 'K1', 'K2'], name='key1')
In [18]: left = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
....: 'B': ['B0', 'B1', 'B2', 'B3'],
....: 'key2': ['K0', 'K1', 'K0', 'K1']},
....: index=left_index)
....:
In [19]: right_index = pd.Index(['K0', 'K1', 'K2', 'K2'], name='key1')
In [20]: right = pd.DataFrame({'C': ['C0', 'C1', 'C2', 'C3'],
....: 'D': ['D0', 'D1', 'D2', 'D3'],
....: 'key2': ['K0', 'K0', 'K0', 'K1']},
....: index=right_index)
....:
In [21]: left.merge(right, on=['key1', 'key2'])
Out[21]:
A B key2 C D
key1
K0 A0 B0 K0 C0 D0
K1 A2 B2 K0 C1 D1
K2 A3 B3 K1 C3 D3
[3 rows x 5 columns]
支持基于列和索引级别的组合进行排序#
传递给 DataFrame.sort_values()
作为 by
参数的字符串现在可以引用列名或索引级别名。这使得可以在不重置索引的情况下,基于索引级别和列的组合对 DataFrame
实例进行排序。请参阅按索引和值排序文档部分。(GH 14353)
# Build MultiIndex
In [22]: idx = pd.MultiIndex.from_tuples([('a', 1), ('a', 2), ('a', 2),
....: ('b', 2), ('b', 1), ('b', 1)])
....:
In [23]: idx.names = ['first', 'second']
# Build DataFrame
In [24]: df_multi = pd.DataFrame({'A': np.arange(6, 0, -1)},
....: index=idx)
....:
In [25]: df_multi
Out[25]:
A
first second
a 1 6
2 5
2 4
b 2 3
1 2
1 1
[6 rows x 1 columns]
# Sort by 'second' (index) and 'A' (column)
In [26]: df_multi.sort_values(by=['second', 'A'])
Out[26]:
A
first second
b 1 1
1 2
a 1 6
b 2 3
a 2 4
2 5
[6 rows x 1 columns]
使用自定义类型扩展 pandas (实验性)#
pandas 现在支持将不一定是 1-D NumPy 数组的类数组对象存储为 DataFrame 中的列或 Series 中的值。这使得第三方库可以实现对 NumPy 类型的扩展,类似于 pandas 实现分类类型、带时区的日期时间、周期和间隔。
作为演示,我们将使用 cyberpandas,它提供了一个用于存储 IP 地址的 IPArray
类型。
In [1]: from cyberpandas import IPArray
In [2]: values = IPArray([
...: 0,
...: 3232235777,
...: 42540766452641154071740215577757643572
...: ])
...:
...:
IPArray
不是一个普通的 1-D NumPy 数组,但由于它是 pandas ExtensionArray
,它可以正确地存储在 pandas 的容器中。
In [3]: ser = pd.Series(values)
In [4]: ser
Out[4]:
0 0.0.0.0
1 192.168.1.1
2 2001:db8:85a3::8a2e:370:7334
dtype: ip
注意 dtype 是 ip
。底层数组的缺失值语义得到了遵守。
In [5]: ser.isna()
Out[5]:
0 True
1 False
2 False
dtype: bool
新关键字 observed
用于在 GroupBy
中排除未观察到的类别#
按分类类型分组会包含输出中未观察到的类别。当按多个分类列分组时,这意味着您将获得所有类别的笛卡尔积,包括没有观察值的组合,这可能导致大量的组。我们添加了一个关键字 observed
来控制此行为,为了向后兼容,它默认为 observed=False
。(GH 14942, GH 8138, GH 15217, GH 17594, GH 8669, GH 20583, GH 20902)
In [27]: cat1 = pd.Categorical(["a", "a", "b", "b"],
....: categories=["a", "b", "z"], ordered=True)
....:
In [28]: cat2 = pd.Categorical(["c", "d", "c", "d"],
....: categories=["c", "d", "y"], ordered=True)
....:
In [29]: df = pd.DataFrame({"A": cat1, "B": cat2, "values": [1, 2, 3, 4]})
In [30]: df['C'] = ['foo', 'bar'] * 2
In [31]: df
Out[31]:
A B values C
0 a c 1 foo
1 a d 2 bar
2 b c 3 foo
3 b d 4 bar
[4 rows x 4 columns]
显示所有值(以前的行为)
In [32]: df.groupby(['A', 'B', 'C'], observed=False).count()
Out[32]:
values
A B C
a c bar 0
foo 1
d bar 1
foo 0
y bar 0
... ...
z c foo 0
d bar 0
foo 0
y bar 0
foo 0
[18 rows x 1 columns]
只显示观察到的值
In [33]: df.groupby(['A', 'B', 'C'], observed=True).count()
Out[33]:
values
A B C
a c foo 1
d bar 1
b c foo 1
d bar 1
[4 rows x 1 columns]
对于透视操作,此行为已经由 dropna
关键字控制。
In [34]: cat1 = pd.Categorical(["a", "a", "b", "b"],
....: categories=["a", "b", "z"], ordered=True)
....:
In [35]: cat2 = pd.Categorical(["c", "d", "c", "d"],
....: categories=["c", "d", "y"], ordered=True)
....:
In [36]: df = pd.DataFrame({"A": cat1, "B": cat2, "values": [1, 2, 3, 4]})
In [37]: df
Out[37]:
A B values
0 a c 1
1 a d 2
2 b c 3
3 b d 4
[4 rows x 3 columns]
In [1]: pd.pivot_table(df, values='values', index=['A', 'B'], dropna=True)
Out[1]:
values
A B
a c 1.0
d 2.0
b c 3.0
d 4.0
In [2]: pd.pivot_table(df, values='values', index=['A', 'B'], dropna=False)
Out[2]:
values
A B
a c 1.0
d 2.0
y NaN
b c 3.0
d 4.0
y NaN
z c NaN
d NaN
y NaN
Rolling/Expanding.apply() 接受 raw=False
以将 Series
传递给函数#
Series.rolling().apply()
、DataFrame.rolling().apply()
、Series.expanding().apply()
和 DataFrame.expanding().apply()
新增了 raw=None
参数。这类似于 DataFame.apply()
。如果此参数为 True
,则可以将 np.ndarray
发送到应用函数。如果为 False
,则将传递一个 Series
。默认值为 None
,这保留了向后兼容性,因此默认将为 True
,发送一个 np.ndarray
。在未来版本中,默认值将更改为 False
,发送一个 Series
。(GH 5071, GH 20584)
In [38]: s = pd.Series(np.arange(5), np.arange(5) + 1)
In [39]: s
Out[39]:
1 0
2 1
3 2
4 3
5 4
Length: 5, dtype: int64
传递一个 Series
In [40]: s.rolling(2, min_periods=1).apply(lambda x: x.iloc[-1], raw=False)
Out[40]:
1 0.0
2 1.0
3 2.0
4 3.0
5 4.0
Length: 5, dtype: float64
模仿原始行为,传递一个 ndarray
In [41]: s.rolling(2, min_periods=1).apply(lambda x: x[-1], raw=True)
Out[41]:
1 0.0
2 1.0
3 2.0
4 3.0
5 4.0
Length: 5, dtype: float64
DataFrame.interpolate
新增 limit_area
kwarg#
DataFrame.interpolate()
新增了 limit_area
参数,以进一步控制替换哪些 NaN
。使用 limit_area='inside'
只填充被有效值包围的 NaN,或使用 limit_area='outside'
只填充现有有效值外部的 NaN
,同时保留内部的 NaN。(GH 16284)请参阅此处完整文档。
In [42]: ser = pd.Series([np.nan, np.nan, 5, np.nan, np.nan,
....: np.nan, 13, np.nan, np.nan])
....:
In [43]: ser
Out[43]:
0 NaN
1 NaN
2 5.0
3 NaN
4 NaN
5 NaN
6 13.0
7 NaN
8 NaN
Length: 9, dtype: float64
在两个方向上填充一个连续的内部值
In [44]: ser.interpolate(limit_direction='both', limit_area='inside', limit=1)
Out[44]:
0 NaN
1 NaN
2 5.0
3 7.0
4 NaN
5 11.0
6 13.0
7 NaN
8 NaN
Length: 9, dtype: float64
向后填充所有连续的外部值
In [45]: ser.interpolate(limit_direction='backward', limit_area='outside')
Out[45]:
0 5.0
1 5.0
2 5.0
3 NaN
4 NaN
5 NaN
6 13.0
7 NaN
8 NaN
Length: 9, dtype: float64
在两个方向上填充所有连续的外部值
In [46]: ser.interpolate(limit_direction='both', limit_area='outside')
Out[46]:
0 5.0
1 5.0
2 5.0
3 NaN
4 NaN
5 NaN
6 13.0
7 13.0
8 13.0
Length: 9, dtype: float64
函数 get_dummies
现在支持 dtype
参数#
get_dummies()
现在接受 dtype
参数,用于指定新列的数据类型。默认值仍为 uint8。(GH 18330)
In [47]: df = pd.DataFrame({'a': [1, 2], 'b': [3, 4], 'c': [5, 6]})
In [48]: pd.get_dummies(df, columns=['c']).dtypes
Out[48]:
a int64
b int64
c_5 bool
c_6 bool
Length: 4, dtype: object
In [49]: pd.get_dummies(df, columns=['c'], dtype=bool).dtypes
Out[49]:
a int64
b int64
c_5 bool
c_6 bool
Length: 4, dtype: object
Timedelta 取模方法#
当与类 Timedelta 对象或数值参数一起操作时,Timedelta
对象现在定义了 mod
(%) 和 divmod
操作。请参阅此处文档。(GH 19365)
In [50]: td = pd.Timedelta(hours=37)
In [51]: td % pd.Timedelta(minutes=45)
Out[51]: Timedelta('0 days 00:15:00')
方法 .rank()
在存在 NaN
值时处理 inf
值#
在以前的版本中,.rank()
会将 inf
元素的排名分配为 NaN
。现在排名计算正确。(GH 6945)
In [52]: s = pd.Series([-np.inf, 0, 1, np.nan, np.inf])
In [53]: s
Out[53]:
0 -inf
1 0.0
2 1.0
3 NaN
4 inf
Length: 5, dtype: float64
以前的行为
In [11]: s.rank()
Out[11]:
0 1.0
1 2.0
2 3.0
3 NaN
4 NaN
dtype: float64
当前行为
In [54]: s.rank()
Out[54]:
0 1.0
1 2.0
2 3.0
3 NaN
4 4.0
Length: 5, dtype: float64
此外,以前如果您将 inf
或 -inf
值与 NaN
值一起排名时,使用 'top' 或 'bottom' 参数时计算不会区分 NaN
和无穷大。
In [55]: s = pd.Series([np.nan, np.nan, -np.inf, -np.inf])
In [56]: s
Out[56]:
0 NaN
1 NaN
2 -inf
3 -inf
Length: 4, dtype: float64
以前的行为
In [15]: s.rank(na_option='top')
Out[15]:
0 2.5
1 2.5
2 2.5
3 2.5
dtype: float64
当前行为
In [57]: s.rank(na_option='top')
Out[57]:
0 1.5
1 1.5
2 3.5
3 3.5
Length: 4, dtype: float64
这些 bug 已被修复
当
method='dense'
和pct=True
时,DataFrame.rank()
和Series.rank()
中的 bug,即未根据不同观察值的数量使用百分位数排名(GH 15630)当
ascending='False'
且存在NaN
时,Series.rank()
和DataFrame.rank()
中的 bug,即无法返回无穷大的正确排名(GH 19538)DataFrameGroupBy.rank()
中的 bug,当无穷大和NaN
都存在时,排名不正确(GH 20561)
Series.str.cat
新增 join
kwarg#
以前,Series.str.cat()
与大多数 pandas
的行为不同,它在连接前不会根据索引对 Series
进行对齐(参见 GH 18657)。该方法现在新增了一个关键字 join
来控制对齐方式,请参见下面的示例和此处。
在 v0.23 版本中,join
默认为 None(表示不对齐),但在未来的 pandas 版本中,此默认值将更改为 'left'
。
In [58]: s = pd.Series(['a', 'b', 'c', 'd'])
In [59]: t = pd.Series(['b', 'd', 'e', 'c'], index=[1, 3, 4, 2])
In [60]: s.str.cat(t)
Out[60]:
0 NaN
1 bb
2 cc
3 dd
Length: 4, dtype: object
In [61]: s.str.cat(t, join='left', na_rep='-')
Out[61]:
0 a-
1 bb
2 cc
3 dd
Length: 4, dtype: object
此外,Series.str.cat()
现在也适用于 CategoricalIndex
(以前会引发 ValueError
;参见 GH 20842)。
DataFrame.astype
支持按列转换为 Categorical
类型#
DataFrame.astype()
现在可以通过提供字符串 'category'
或 CategoricalDtype
来执行按列转换为 Categorical
类型。以前,尝试这样做会引发 NotImplementedError
。更多详情和示例请参阅文档的对象创建部分。(GH 12860, GH 18099)
提供字符串 'category'
将执行按列转换,只有在给定列中出现的标签才被设置为类别。
In [62]: df = pd.DataFrame({'A': list('abca'), 'B': list('bccd')})
In [63]: df = df.astype('category')
In [64]: df['A'].dtype
Out[64]: CategoricalDtype(categories=['a', 'b', 'c'], ordered=False, categories_dtype=object)
In [65]: df['B'].dtype
Out[65]: CategoricalDtype(categories=['b', 'c', 'd'], ordered=False, categories_dtype=object)
提供 CategoricalDtype
将使每列中的类别与提供的 dtype 一致。
In [66]: from pandas.api.types import CategoricalDtype
In [67]: df = pd.DataFrame({'A': list('abca'), 'B': list('bccd')})
In [68]: cdt = CategoricalDtype(categories=list('abcd'), ordered=True)
In [69]: df = df.astype(cdt)
In [70]: df['A'].dtype
Out[70]: CategoricalDtype(categories=['a', 'b', 'c', 'd'], ordered=True, categories_dtype=object)
In [71]: df['B'].dtype
Out[71]: CategoricalDtype(categories=['a', 'b', 'c', 'd'], ordered=True, categories_dtype=object)
其他增强功能#
一元运算符
+
现在允许用于Series
和DataFrame
作为数值运算符(GH 16073)。更好地支持使用
xlsxwriter
引擎的to_excel()
输出。(GH 16149)pandas.tseries.frequencies.to_offset()
现在接受开头的 '+' 号,例如 '+1h'。(GH 18171)MultiIndex.unique()
现在支持level=
参数,用于从特定索引级别获取唯一值(GH 17896)。pandas.io.formats.style.Styler
现在拥有方法hide_index()
,用于确定索引是否会在输出中呈现(GH 14194)。pandas.io.formats.style.Styler
现在拥有方法hide_columns()
,用于确定列是否会在输出中隐藏(GH 14194)。改进了
to_datetime()
在传递不可转换值给unit=
参数时引发的ValueError
的措辞 (GH 14350)Series.fillna()
现在接受 Series 或 dict 作为分类 dtype 的value
参数 (GH 17033)pandas.read_clipboard()
更新为使用 qtpy,并依次回退到 PyQt5 和 PyQt4,增加了对 Python3 和多种 python-qt 绑定的兼容性 (GH 17722)改进了
read_csv()
在usecols
参数无法匹配所有列时引发的ValueError
的措辞。(GH 17301)DataFrame.corrwith()
现在在传入 Series 时会静默删除非数值列。之前,会引发异常 (GH 18570)。IntervalIndex
现在支持时区感知的Interval
对象 (GH 18537, GH 18538)Series()
/DataFrame()
的 Tab 补全现在也返回MultiIndex()
第一级的标识符。(GH 16326)read_excel()
新增了nrows
参数 (GH 16645)DataFrame.append()
现在在更多情况下可以保留调用 DataFrame 列的类型(例如,如果两者都是CategoricalIndex
)(GH 18359)DataFrame.to_json()
和Series.to_json()
现在接受一个index
参数,允许用户在 JSON 输出中排除索引 (GH 17394)IntervalIndex.to_tuples()
新增了na_tuple
参数,用于控制 NA 是作为 NA 的元组返回,还是作为 NA 本身返回 (GH 18756)Categorical.rename_categories
、CategoricalIndex.rename_categories
和Series.cat.rename_categories
现在可以接受可调用对象作为其参数 (GH 18862)Interval
和IntervalIndex
新增了length
属性 (GH 18789)Resampler
对象现在有了功能正常的Resampler.pipe
方法。之前,对pipe
的调用会被重定向到mean
方法 (GH 17905)。is_scalar()
现在对DateOffset
对象返回True
(GH 18943)。DataFrame.pivot()
现在接受列表作为values=
关键字参数 (GH 17160)。添加了
pandas.api.extensions.register_dataframe_accessor()
、pandas.api.extensions.register_series_accessor()
和pandas.api.extensions.register_index_accessor()
,这些是用于 pandas 下游库在 pandas 对象上注册自定义访问器(例如.cat
)的访问器。详情请参见 注册自定义访问器 (GH 14781)。IntervalIndex.astype
现在在传入IntervalDtype
时支持子类型之间的转换 (GH 19197)IntervalIndex
及其相关的构造方法(from_arrays
、from_breaks
、from_tuples
)新增了dtype
参数 (GH 19262)添加了
SeriesGroupBy.is_monotonic_increasing()
和SeriesGroupBy.is_monotonic_decreasing()
(GH 17015)对于子类化的
DataFrames
,DataFrame.apply()
现在在将数据传递给应用函数时,将保留Series
子类(如果已定义)(GH 19822)DataFrame.from_dict()
现在接受一个columns
参数,当使用orient='index'
时,该参数可用于指定列名 (GH 18529)添加了选项
display.html.use_mathjax
,以便在Jupyter
notebook 中渲染表格时可以禁用 MathJax (GH 19856, GH 19824)DataFrame.replace()
现在支持method
参数,当to_replace
是标量、列表或元组且value
为None
时,该参数可用于指定替换方法 (GH 19632)Timestamp.month_name()
、DatetimeIndex.month_name()
和Series.dt.month_name()
现在可用 (GH 12805)Timestamp.day_name()
和DatetimeIndex.day_name()
现在可用,用于返回具有指定区域设置的日期名称 (GH 12806)如果底层连接支持,
DataFrame.to_sql()
现在会执行多值插入,而不是逐行插入。支持多值插入的SQLAlchemy
方言包括:mysql
、postgresql
、sqlite
以及任何具有supports_multivalues_insert
的方言。(GH 14315, GH 8953)read_html()
现在接受一个displayed_only
关键字参数,用于控制是否解析隐藏元素(默认为True
)(GH 20027)read_html()
现在会读取中的所有
元素,而不仅仅是第一个。(GH 20690)
Rolling.quantile()
和Expanding.quantile()
现在接受interpolation
关键字参数,默认为linear
(GH 20497)通过在
DataFrame.to_pickle()
、Series.to_pickle()
、DataFrame.to_csv()
、Series.to_csv()
、DataFrame.to_json()
、Series.to_json()
中指定compression=zip
支持 zip 压缩。(GH 17778)WeekOfMonth
构造函数现在支持n=0
(GH 20517)。对于 Python>=3.5,
DataFrame
和Series
现在支持矩阵乘法 (@
) 运算符 (GH 10259)更新了
DataFrame.to_gbq()
和pandas.read_gbq()
的签名和文档,以反映 pandas-gbq 库 0.4.0 版本的变化。增加了到 pandas-gbq 库的 intersphinx 映射。(GH 20564)添加了用于导出 Stata dta 文件的新写入器,版本 117,名为
StataWriter117
。此格式支持导出长度达 2,000,000 个字符的字符串 (GH 16450)to_hdf()
和read_hdf()
现在接受errors
关键字参数,用于控制编码错误处理 (GH 20835)cut()
新增了duplicates='raise'|'drop'
选项,用于控制是否对重复的边界引发错误 (GH 20947)如果指定了
start
、stop
和periods
但未指定freq
,date_range()
、timedelta_range()
和interval_range()
现在返回一个线性间隔的索引。(GH 20808, GH 20983, GH 20976)向后不兼容的 API 变更#
依赖项的最低版本已提高#
我们已更新了依赖项的最低支持版本 (GH 15184)。如果安装,我们现在要求
软件包
最低版本
要求
问题
python-dateutil
2.5.0
X
openpyxl
2.4.0
beautifulsoup4
4.2.1
setuptools
24.2.0
在 Python 3.6+ 中,从字典实例化将保留字典的插入顺序#
在 Python 3.6 之前,Python 中的字典没有正式定义的顺序。对于 Python 3.6 及更高版本,字典按插入顺序排序,参见 PEP 468。当您使用 Python 3.6 或更高版本并从字典创建
Series
或DataFrame
时,pandas 将使用字典的插入顺序。(GH 19884)之前的行为(以及 Python < 3.6 时的当前行为)
In [16]: pd.Series({'Income': 2000, ....: 'Expenses': -1500, ....: 'Taxes': -200, ....: 'Net result': 300}) Out[16]: Expenses -1500 Income 2000 Net result 300 Taxes -200 dtype: int64
请注意,上面的 Series 按索引值按字母顺序排序。
新的行为(适用于 Python >= 3.6)
In [72]: pd.Series({'Income': 2000, ....: 'Expenses': -1500, ....: 'Taxes': -200, ....: 'Net result': 300}) ....: Out[72]: Income 2000 Expenses -1500 Taxes -200 Net result 300 Length: 4, dtype: int64
注意 Series 现在按插入顺序排序。此新行为适用于所有相关的 pandas 类型(
Series
、DataFrame
、SparseSeries
和SparseDataFrame
)。如果您希望在使用 Python >= 3.6 时保留旧行为,可以使用
.sort_index()
In [73]: pd.Series({'Income': 2000, ....: 'Expenses': -1500, ....: 'Taxes': -200, ....: 'Net result': 300}).sort_index() ....: Out[73]: Expenses -1500 Income 2000 Net result 300 Taxes -200 Length: 4, dtype: int64
弃用 Panel#
Panel
在 0.20.x 版本中被弃用,显示为DeprecationWarning
。现在使用Panel
将显示FutureWarning
。表示 3 维数据的推荐方法是通过to_frame()
方法在DataFrame
上使用MultiIndex
,或使用 xarray package。pandas 提供了一个to_xarray()
方法来自动化此转换 (GH 13563, GH 18324)。In [75]: import pandas._testing as tm In [76]: p = tm.makePanel() In [77]: p Out[77]: <class 'pandas.core.panel.Panel'> Dimensions: 3 (items) x 3 (major_axis) x 4 (minor_axis) Items axis: ItemA to ItemC Major_axis axis: 2000-01-03 00:00:00 to 2000-01-05 00:00:00 Minor_axis axis: A to D
转换为 MultiIndex DataFrame
In [78]: p.to_frame() Out[78]: ItemA ItemB ItemC major minor 2000-01-03 A 0.469112 0.721555 0.404705 B -1.135632 0.271860 -1.039268 C 0.119209 0.276232 -1.344312 D -2.104569 0.113648 -0.109050 2000-01-04 A -0.282863 -0.706771 0.577046 B 1.212112 -0.424972 -0.370647 C -1.044236 -1.087401 0.844885 D -0.494929 -1.478427 1.643563 2000-01-05 A -1.509059 -1.039575 -1.715002 B -0.173215 0.567020 -1.157892 C -0.861849 -0.673690 1.075770 D 1.071804 0.524988 -1.469388 [12 rows x 3 columns]
转换为 xarray DataArray
In [79]: p.to_xarray() Out[79]: <xarray.DataArray (items: 3, major_axis: 3, minor_axis: 4)> array([[[ 0.469112, -1.135632, 0.119209, -2.104569], [-0.282863, 1.212112, -1.044236, -0.494929], [-1.509059, -0.173215, -0.861849, 1.071804]], [[ 0.721555, 0.27186 , 0.276232, 0.113648], [-0.706771, -0.424972, -1.087401, -1.478427], [-1.039575, 0.56702 , -0.67369 , 0.524988]], [[ 0.404705, -1.039268, -1.344312, -0.10905 ], [ 0.577046, -0.370647, 0.844885, 1.643563], [-1.715002, -1.157892, 1.07577 , -1.469388]]]) Coordinates: * items (items) object 'ItemA' 'ItemB' 'ItemC' * major_axis (major_axis) datetime64[ns] 2000-01-03 2000-01-04 2000-01-05 * minor_axis (minor_axis) object 'A' 'B' 'C' 'D'
移除 pandas.core.common#
以下错误和警告消息已从
pandas.core.common
中移除 (GH 13634, GH 19769)PerformanceWarning
UnsupportedFunctionCall
UnsortedIndexError
AbstractMethodError
这些可以从
pandas.errors
中导入(自 0.19.0 版本起)。使
DataFrame.apply
输出一致的更改#DataFrame.apply()
在应用返回 list-like 对象且axis=1
的任意用户定义函数时存在不一致。现已解决了一些错误和不一致之处。如果应用的函数返回 Series,则 pandas 将返回 DataFrame;否则将返回 Series,这包括返回 list-like 对象(例如tuple
或list
)的情况 (GH 16353, GH 17437, GH 17970, GH 17348, GH 17892, GH 18573, GH 17602, GH 18775, GH 18901, GH 18919)。In [74]: df = pd.DataFrame(np.tile(np.arange(3), 6).reshape(6, -1) + 1, ....: columns=['A', 'B', 'C']) ....: In [75]: df Out[75]: A B C 0 1 2 3 1 1 2 3 2 1 2 3 3 1 2 3 4 1 2 3 5 1 2 3 [6 rows x 3 columns]
之前的行为:如果返回的形状恰好与原始列的长度匹配,则会返回
DataFrame
。如果返回的形状不匹配,则返回包含列表的Series
。In [3]: df.apply(lambda x: [1, 2, 3], axis=1) Out[3]: A B C 0 1 2 3 1 1 2 3 2 1 2 3 3 1 2 3 4 1 2 3 5 1 2 3 In [4]: df.apply(lambda x: [1, 2], axis=1) Out[4]: 0 [1, 2] 1 [1, 2] 2 [1, 2] 3 [1, 2] 4 [1, 2] 5 [1, 2] dtype: object
新的行为:当应用的函数返回 list-like 对象时,现在将 *始终* 返回
Series
。In [76]: df.apply(lambda x: [1, 2, 3], axis=1) Out[76]: 0 [1, 2, 3] 1 [1, 2, 3] 2 [1, 2, 3] 3 [1, 2, 3] 4 [1, 2, 3] 5 [1, 2, 3] Length: 6, dtype: object In [77]: df.apply(lambda x: [1, 2], axis=1) Out[77]: 0 [1, 2] 1 [1, 2] 2 [1, 2] 3 [1, 2] 4 [1, 2] 5 [1, 2] Length: 6, dtype: object
要展开列,可以使用
result_type='expand'
In [78]: df.apply(lambda x: [1, 2, 3], axis=1, result_type='expand') Out[78]: 0 1 2 0 1 2 3 1 1 2 3 2 1 2 3 3 1 2 3 4 1 2 3 5 1 2 3 [6 rows x 3 columns]
要将结果广播到原始列(长度正确的 list-like 对象的旧行为),可以使用
result_type='broadcast'
。形状必须与原始列匹配。In [79]: df.apply(lambda x: [1, 2, 3], axis=1, result_type='broadcast') Out[79]: A B C 0 1 2 3 1 1 2 3 2 1 2 3 3 1 2 3 4 1 2 3 5 1 2 3 [6 rows x 3 columns]
返回
Series
允许控制确切的返回结构和列名In [80]: df.apply(lambda x: pd.Series([1, 2, 3], index=['D', 'E', 'F']), axis=1) Out[80]: D E F 0 1 2 3 1 1 2 3 2 1 2 3 3 1 2 3 4 1 2 3 5 1 2 3 [6 rows x 3 columns]
连接将不再排序#
在 pandas 的未来版本中,当非连接轴未对齐时,
pandas.concat()
将不再对其进行排序。当前行为与之前相同(排序),但现在当未指定sort
且非连接轴未对齐时,会发出警告 (GH 4588)。In [81]: df1 = pd.DataFrame({"a": [1, 2], "b": [1, 2]}, columns=['b', 'a']) In [82]: df2 = pd.DataFrame({"a": [4, 5]}) In [83]: pd.concat([df1, df2]) Out[83]: b a 0 1.0 1 1 2.0 2 0 NaN 4 1 NaN 5 [4 rows x 2 columns]
要保留之前的行为(排序)并抑制警告,请传递
sort=True
In [84]: pd.concat([df1, df2], sort=True) Out[84]: a b 0 1 1.0 1 2 2.0 0 4 NaN 1 5 NaN [4 rows x 2 columns]
要接受未来的行为(不排序),请传递
sort=False
请注意,此更改也适用于
DataFrame.append()
,后者也新增了sort
关键字来控制此行为。构建更改#
Index 除以零正确填充#
对
Index
及其子类执行除法运算时,正数除以零现在将填充np.inf
,负数除以零填充-np.inf
,以及0 / 0
填充np.nan
。这与现有的Series
行为一致。(GH 19322, GH 19347)以前的行为
In [6]: index = pd.Int64Index([-1, 0, 1]) In [7]: index / 0 Out[7]: Int64Index([0, 0, 0], dtype='int64') # Previous behavior yielded different results depending on the type of zero in the divisor In [8]: index / 0.0 Out[8]: Float64Index([-inf, nan, inf], dtype='float64') In [9]: index = pd.UInt64Index([0, 1]) In [10]: index / np.array([0, 0], dtype=np.uint64) Out[10]: UInt64Index([0, 0], dtype='uint64') In [11]: pd.RangeIndex(1, 5) / 0 ZeroDivisionError: integer division or modulo by zero
当前行为
In [12]: index = pd.Int64Index([-1, 0, 1]) # division by zero gives -infinity where negative, # +infinity where positive, and NaN for 0 / 0 In [13]: index / 0 # The result of division by zero should not depend on # whether the zero is int or float In [14]: index / 0.0 In [15]: index = pd.UInt64Index([0, 1]) In [16]: index / np.array([0, 0], dtype=np.uint64) In [17]: pd.RangeIndex(1, 5) / 0
从字符串中提取匹配模式#
默认情况下,使用
str.extract()
从字符串中提取匹配模式时,如果只提取一个组,则返回Series
(如果提取多个组,则返回DataFrame
)。从 pandas 0.23.0 开始,str.extract()
始终返回DataFrame
,除非将expand
设置为False
。最后,None
曾经是expand
参数的可接受值(等同于False
),但现在会引发ValueError
。(GH 11386)以前的行为
In [1]: s = pd.Series(['number 10', '12 eggs']) In [2]: extracted = s.str.extract(r'.*(\d\d).*') In [3]: extracted Out [3]: 0 10 1 12 dtype: object In [4]: type(extracted) Out [4]: pandas.core.series.Series
新的行为
In [85]: s = pd.Series(['number 10', '12 eggs']) In [86]: extracted = s.str.extract(r'.*(\d\d).*') In [87]: extracted Out[87]: 0 0 10 1 12 [2 rows x 1 columns] In [88]: type(extracted) Out[88]: pandas.core.frame.DataFrame
要恢复先前的行为,只需将
expand
设置为False
In [89]: s = pd.Series(['number 10', '12 eggs']) In [90]: extracted = s.str.extract(r'.*(\d\d).*', expand=False) In [91]: extracted Out[91]: 0 10 1 12 Length: 2, dtype: object In [92]: type(extracted) Out[92]: pandas.core.series.Series
CategoricalDtype
的ordered
参数的默认值#CategoricalDtype
的ordered
参数的默认值已从False
更改为None
,以允许更新categories
而不影响ordered
。对于下游对象(例如Categorical
),行为应保持一致。(GH 18790)在以前的版本中,
ordered
参数的默认值为False
。当用户尝试更新categories
时,如果未明确指定ordered
,它会静默地默认为False
,这可能导致ordered
参数意外地从True
更改为False
。ordered=None
的新行为是保留ordered
的现有值。新的行为
In [2]: from pandas.api.types import CategoricalDtype In [3]: cat = pd.Categorical(list('abcaba'), ordered=True, categories=list('cba')) In [4]: cat Out[4]: [a, b, c, a, b, a] Categories (3, object): [c < b < a] In [5]: cdt = CategoricalDtype(categories=list('cbad')) In [6]: cat.astype(cdt) Out[6]: [a, b, c, a, b, a] Categories (4, object): [c < b < a < d]
请注意,在上面的示例中,转换后的
Categorical
保留了ordered=True
。如果ordered
的默认值保持为False
,则转换后的Categorical
将变为无序,尽管从未明确指定ordered=False
。要更改ordered
的值,请将其明确传递给新的 dtype,例如CategoricalDtype(categories=list('cbad'), ordered=False)
。请注意,上面讨论的
ordered
的意外转换在早期版本中并未出现,原因是由于阻止astype
进行任何类别的到类别转换的独立错误(GH 10696、GH 18593)。这些错误在此版本中已修复,并促使更改了ordered
的默认值。更好的终端 DataFrame 美观打印#
以前,最大列数的默认值是
pd.options.display.max_columns=20
。这意味着相对较宽的数据帧无法在终端宽度内显示,pandas 会引入换行符来显示这 20 列。这导致输出相对难以阅读如果 Python 在终端中运行,现在会自动确定最大列数,以便打印的数据帧适合当前终端宽度(
pd.options.display.max_columns=0
)(GH 17023)。如果 Python 作为 Jupyter 内核(例如 Jupyter QtConsole 或 Jupyter notebook,以及许多 IDE)运行,则无法自动推断此值,因此设置为20
,与早期版本一样。在终端中,这会产生更好的输出请注意,如果您不喜欢新的默认设置,可以随时自行设置此选项。要恢复旧设置,可以运行以下行
pd.options.display.max_columns = 20
日期时间类 API 更改#
Timedelta
默认构造函数现在接受ISO 8601 Duration
字符串作为参数(GH 19040)从
dtype='datetime64[ns]'
的Series
中减去NaT
会返回dtype='timedelta64[ns]'
的Series
,而不是dtype='datetime64[ns]'
(GH 18808)从
TimedeltaIndex
中添加或减去NaT
将返回TimedeltaIndex
而不是DatetimeIndex
(GH 19124)当索引对象的频率是
None
时,DatetimeIndex.shift()
和TimedeltaIndex.shift()
现在会引发NullFrequencyError
(它是ValueError
的子类,在早期版本中会引发ValueError
)(GH 19147)从
dtype='timedelta64[ns]'
的Series
中添加或减去NaN
将引发TypeError
,而不是将NaN
视为NaT
(GH 19274)NaT
与datetime.timedelta
进行除法运算现在将返回NaN
,而不是引发异常(GH 17876)具有
dtype='datetime64[ns]'
的Series
与PeriodIndex
之间的运算将正确地引发TypeError
(GH 18850)具有时区感知
dtype='datetime64[ns]'
的Series
与时区不匹配的Series
之间的减法将引发TypeError
,而不是ValueError
(GH 18817)CacheableOffset
和WeekDay
不再在pandas.tseries.offsets
模块中可用(GH 17830)pandas.tseries.frequencies.get_freq_group()
和pandas.tseries.frequencies.DAYS
已从公共 API 中移除(GH 18034)Series.truncate()
和DataFrame.truncate()
如果索引未排序,将引发ValueError
,而不是无用的KeyError
(GH 17935)当索引不是
DatetimeIndex
时,Series.first
和DataFrame.first
现在会引发TypeError
,而不是NotImplementedError
(GH 20725)。当索引不是
DatetimeIndex
时,Series.last
和DataFrame.last
现在会引发TypeError
,而不是NotImplementedError
(GH 20725)。限制了
DateOffset
关键字参数。以前,DateOffset
子类允许任意关键字参数,这可能导致意外行为。现在,只接受有效参数。(GH 17176,GH 18226)。pandas.merge()
在尝试合并时区感知和时区不感知列时提供更具信息量的错误消息(GH 15800)对于
freq=None
的DatetimeIndex
和TimedeltaIndex
,添加或减去整数 dtype 数组或Index
将引发NullFrequencyError
,而不是TypeError
(GH 19895)在实例化后设置
DatetimeIndex
的tz
属性时,现在将引发AttributeError
(GH 3746)具有
pytz
时区的DatetimeIndex
现在将返回一致的pytz
时区(GH 18595)
其他 API 更改#
Series.astype()
和Index.astype()
使用不兼容的 dtype 时,现在会引发TypeError
,而不是ValueError
(GH 18231)使用对象 dtype 的时区感知 datetime 和指定了
dtype=object
构建Series
时,现在将返回对象 dtype 的Series
,以前会推断 datetime dtype(GH 18231)从空
dict
构建的dtype=category
的Series
现在将具有dtype=object
的 categories,而不是dtype=float64
,这与传递空列表的情况一致(GH 18515)MultiIndex
中所有为 NaN 的 level 现在被分配float
dtype,而不是object
,从而与Index
保持一致(GH 17929)。MultiIndex
的 level 名称(非 None 时)现在要求是唯一的:尝试创建带有重复名称的MultiIndex
将引发ValueError
(GH 18872)使用不可哈希的
name
/names
构建和重命名Index
/MultiIndex
现在将引发TypeError
(GH 20527)Index.map()
现在可以接受Series
和 dictionary 输入对象(GH 12756、GH 18482、GH 18509)。DataFrame.unstack()
现在默认为object
列填充np.nan
。(GH 12815)IntervalIndex
构造函数如果在输入数据的推断闭合方式与closed
参数冲突时将引发异常(GH 18421)向索引插入缺失值现在适用于所有类型的索引,并且无论传入的类型如何,都会自动插入正确类型的缺失值(
NaN
、NaT
等)(GH 18295)使用重复标签创建
MultiIndex
时,现在会引发ValueError
。(GH 17464)Series.fillna()
在将列表、元组或 DataFrame 作为value
传递时,现在会引发TypeError
,而不是ValueError
(GH 18293)pandas.DataFrame.merge()
在合并int
和float
列时,不再将float
列转换为object
(GH 16572)pandas.merge()
在尝试合并不兼容的数据类型时,现在会引发ValueError
(GH 9780)UInt64Index
的默认 NA 值已从 0 更改为NaN
,这会影响使用 NA 进行屏蔽的方法,例如UInt64Index.where()
(GH 18398)重构了
setup.py
,使用find_packages
而不是明确列出所有子包(GH 18535)重新排列了
read_excel()
中的关键字参数顺序,以与read_csv()
对齐(GH 16672)wide_to_long()
以前将数字类后缀保留为object
dtype。现在如果可能,会将它们转换为数字(GH 17627)在
read_excel()
中,comment
参数现在作为命名参数公开(GH 18735)重新排列了
read_excel()
中的关键字参数顺序,以与read_csv()
对齐(GH 16672)选项
html.border
和mode.use_inf_as_null
在早期版本中已被弃用,现在它们将显示FutureWarning
,而不是DeprecationWarning
(GH 19003)IntervalIndex
和IntervalDtype
不再支持 categorical、object 和 string 子类型(GH 19016)无论子类型如何,
IntervalDtype
现在与'interval'
比较时返回True
;无论子类型如何,IntervalDtype.name
现在返回'interval'
(GH 18980)在
drop()
、drop()
、drop()
、drop()
中删除具有重复项的轴中的不存在元素时,现在会引发KeyError
,而不是ValueError
(GH 19186)Series.to_csv()
现在接受一个compression
参数,其工作方式与DataFrame.to_csv()
中的compression
参数相同(GH 18958)IntervalIndex
与不兼容索引类型之间的集合运算(并集、差集等)现在会引发TypeError
,而不是ValueError
(GH 19329)DateOffset
对象现在以更简单的方式呈现,例如<DateOffset: days=1>
而不是<DateOffset: kwds={'days': 1}>
(GH 19403)Categorical.fillna
现在验证其value
和method
关键字参数。当同时指定或都不指定时,现在会引发异常,这与Series.fillna()
的行为一致(GH 19682)pd.to_datetime('today')
现在返回一个 datetime 对象,这与pd.Timestamp('today')
一致;以前pd.to_datetime('today')
返回一个.normalized()
datetime 对象(GH 19935)Series.str.replace()
现在接受一个可选的regex
关键字参数,当设置为False
时,使用字面字符串替换而不是正则表达式替换(GH 16808)DatetimeIndex.strftime()
和PeriodIndex.strftime()
现在返回一个Index
而不是一个 numpy 数组,以与类似的访问器保持一致 (GH 20127)对于只包含 int 和 float 列的 DataFrame,
DataFrame.to_dict()
在使用orient='index'
时不再将 int 列转换为 float (GH 18580)传递给
Series.rolling().aggregate()
、DataFrame.rolling().aggregate()
或其扩展版本 (`expanding`) 的用户自定义函数现在将**始终**传递一个Series
,而不是np.array
;.apply()
只有raw
关键字,请参阅此处。这与 pandas 中.aggregate()
的签名保持一致 (GH 20584)Rolling 和 Expanding 类型在迭代时引发
NotImplementedError
(GH 11704)。
弃用#
Series.from_array
和SparseSeries.from_array
已弃用。请改用普通的构造函数Series(..)
和SparseSeries(..)
(GH 18213)。DataFrame.as_matrix
已弃用。请改用DataFrame.values
(GH 18458)。Series.asobject
、DatetimeIndex.asobject
、PeriodIndex.asobject
和TimeDeltaIndex.asobject
已弃用。请改用.astype(object)
(GH 18572)现在按键的元组进行分组会发出
FutureWarning
并已弃用。将来,传递给'by'
的元组将始终引用一个实际的元组作为单个键,而不是将元组视为多个键。要保留以前的行为,请使用列表而不是元组 (GH 18314)Series.valid
已弃用。请改用Series.dropna()
(GH 18800)。read_excel()
已弃用skip_footer
参数。请改用skipfooter
(GH 18836)为了与
read_excel()
保持一致,ExcelFile.parse()
已弃用sheetname
,推荐使用sheet_name
(GH 20920)。is_copy
属性已弃用,并将在未来的版本中移除 (GH 18801)。IntervalIndex.from_intervals
已弃用,推荐使用IntervalIndex
构造函数 (GH 19263)DataFrame.from_items
已弃用。请改用DataFrame.from_dict()
,如果需要保留键的顺序,请使用DataFrame.from_dict(OrderedDict())
(GH 17320, GH 17312)使用包含一些缺失键的列表对
MultiIndex
或FloatIndex
进行索引现在将显示FutureWarning
,这与其他类型的索引一致 (GH 17758)。.apply()
的broadcast
参数已弃用,推荐使用result_type='broadcast'
(GH 18577).apply()
的reduce
参数已弃用,推荐使用result_type='reduce'
(GH 18577)factorize()
的order
参数已弃用,并将在未来的版本中移除 (GH 19727)Timestamp.weekday_name
、DatetimeIndex.weekday_name
和Series.dt.weekday_name
已弃用,推荐使用Timestamp.day_name()
、DatetimeIndex.day_name()
和Series.dt.day_name()
(GH 12806)pandas.tseries.plotting.tsplot
已弃用。请改用Series.plot()
(GH 18627)Index.summary()
已弃用,并将在未来的版本中移除 (GH 18217)NDFrame.get_ftype_counts()
已弃用,并将在未来的版本中移除 (GH 18243)DataFrame.to_records()
中的convert_datetime64
参数已弃用,并将在未来的版本中移除。导致引入此参数的 NumPy 错误已解决。此参数的默认值也已从True
更改为None
(GH 18160)。Series.rolling().apply()
、DataFrame.rolling().apply()
、Series.expanding().apply()
和DataFrame.expanding().apply()
已弃用默认传递np.array
的行为。现在需要传递新的raw
参数来明确指定传递的内容 (GH 20584)Series
和Index
类的data
、base
、strides
、flags
和itemsize
属性已弃用,并将在未来的版本中移除 (GH 20419)。DatetimeIndex.offset
已弃用。请改用DatetimeIndex.freq
(GH 20716)整数 ndarray 与
Timedelta
之间的整除已弃用。请改用Timedelta.value
进行除法 (GH 19761)设置
PeriodIndex.freq
(以前不能保证正确工作)已弃用。请改用PeriodIndex.asfreq()
(GH 20678)Index.get_duplicates()
已弃用,并将在未来的版本中移除 (GH 20239)Categorical.take
中负索引的先前默认行为已弃用。未来的版本中,其含义将从表示缺失值变为表示从右侧开始的位置索引。未来的行为与Series.take()
一致 (GH 20664)。在
DataFrame.dropna()
中向axis
参数传递多个轴已弃用,并将在未来的版本中移除 (GH 20987)
移除先前版本的弃用/变更#
针对过时用法
Categorical(codes, categories)
的警告(例如当Categorical()
的前两个参数具有不同数据类型时发出,并建议使用Categorical.from_codes
)现在已被移除 (GH 8074)MultiIndex
的levels
和labels
属性不能再直接设置 (GH 4039)。pd.tseries.util.pivot_annual
已移除(自 v0.19 起弃用)。请改用pivot_table
(GH 18370)pd.tseries.util.isleapyear
已移除(自 v0.19 起弃用)。请改用 Datetime 类似对象的.is_leap_year
属性 (GH 18370)pd.ordered_merge
已移除(自 v0.19 起弃用)。请改用pd.merge_ordered
(GH 18459)SparseList
类已移除 (GH 14007)pandas.io.wb
和pandas.io.data
存根模块已移除 (GH 13735)Categorical.from_array
已移除 (GH 13854)DataFrame
和Series
的rolling
、expanding
和ewm
方法已移除freq
和how
参数(自 v0.18 起弃用)。请改为在调用方法之前进行重采样 (GH 18601 & GH 18668)DatetimeIndex.to_datetime
、Timestamp.to_datetime
、PeriodIndex.to_datetime
和Index.to_datetime
已移除 (GH 8254, GH 14096, GH 14113)read_csv()
已移除skip_footer
参数 (GH 13386)read_csv()
已移除as_recarray
参数 (GH 13373)read_csv()
已移除buffer_lines
参数 (GH 13360)read_csv()
已移除compact_ints
和use_unsigned
参数 (GH 13323)Timestamp
类已移除offset
属性,推荐使用freq
(GH 13593)Series
、Categorical
和Index
类已移除reshape
方法 (GH 13012)pandas.tseries.frequencies.get_standard_freq
已移除,推荐使用pandas.tseries.frequencies.to_offset(freq).rule_code
(GH 13874)pandas.tseries.frequencies.to_offset
已移除freqstr
关键字,推荐使用freq
(GH 13874)Panel4D
和PanelND
类已移除 (GH 13776)Panel
类已移除to_long
和toLong
方法 (GH 19077)选项
display.line_with
和display.height
已移除,分别推荐使用display.width
和display.max_rows
(GH 4391, GH 19107)Categorical
类的labels
属性已移除,推荐使用Categorical.codes
(GH 7768)to_sql()
方法已移除flavor
参数 (GH 13611)模块
pandas.tools.hashing
和pandas.util.hashing
已移除 (GH 16223)顶层函数
pd.rolling_*
、pd.expanding_*
和pd.ewm*
已移除(自 v0.18 起弃用)。请改用DataFrame
/Series
方法rolling
、expanding
和ewm
(GH 18723)从
pandas.core.common
导入函数(如is_datetime64_dtype
)现已移除。这些函数位于pandas.api.types
中。(GH 13634, GH 19769)Series.tz_localize()
、DatetimeIndex.tz_localize()
和DatetimeIndex
中的infer_dst
关键字已移除。infer_dst=True
等同于ambiguous='infer'
,infer_dst=False
等同于ambiguous='raise'
(GH 7963)。当 `.resample()` 从 v0.18.0 中像 `.groupby()` 一样从即时操作更改为延迟操作时,我们引入了兼容性(带有
FutureWarning
),以便操作能继续工作。现在此兼容性已完全移除,因此Resampler
将不再转发兼容操作 (GH 20554)移除
.replace()
中长期弃用的axis=None
参数 (GH 20271)
性能改进#
Series
或DataFrame
上的索引器不再创建引用循环 (GH 17956)向
to_datetime()
添加了一个关键字参数cache
,它提高了转换重复日期时间参数的性能 (GH 11665)DateOffset
算术运算性能得到改进 (GH 18218)通过对底层方法进行向量化,将
Timedelta
对象组成的 Series 转换为天、秒等操作的速度得到提升 (GH 18092)使用
Series
/dict
输入提高了.map()
的性能 (GH 15081)已移除
Timedelta
对象的days
、seconds
和microseconds
重写属性,转而利用 Python 内置的版本 (GH 18242)在某些情况下,Series 构造将减少输入数据副本的数量 (GH 17449)
提高了
Series.dt.date()
和DatetimeIndex.date()
的性能 (GH 18058)提高了
Series.dt.time()
和DatetimeIndex.time()
的性能 (GH 18461)提高了
IntervalIndex.symmetric_difference()
的性能 (GH 18475)改进了
DatetimeIndex
和Series
算术运算在 Business-Month 和 Business-Quarter 频率下的性能 (GH 18489)Series()
/DataFrame()
的 tab 补全限制为 100 个值,以提高性能。 (GH 18587)改进了未安装 bottleneck 时,
DataFrame.median()
在axis=1
参数下的性能 (GH 16468)改进了
MultiIndex.get_loc()
在大型索引上的性能,代价是小型索引性能略有下降 (GH 18519)改进了
MultiIndex.remove_unused_levels()
在没有未使用的层级时的性能,代价是在有未使用的层级时性能略有下降 (GH 19289)改进了
Index.get_loc()
在非唯一索引上的性能 (GH 19478)改进了成对
.rolling()
和.expanding()
在.cov()
和.corr()
操作中的性能 (GH 17917)改进了
GroupBy.rank()
的性能 (GH 15779)改进了可变窗口
.rolling()
在.min()
和.max()
上的性能 (GH 19521)改进了
GroupBy.ffill()
和GroupBy.bfill()
的性能 (GH 11296)改进了
GroupBy.any()
和GroupBy.all()
的性能 (GH 15435)改进了
GroupBy.pct_change()
的性能 (GH 19165)改进了
Series.isin()
在 categorical dtypes 情况下的性能 (GH 20003)改进了当 Series 具有某些索引类型时,
getattr(Series, attr)
的性能。这体现在大型 Series 使用DatetimeIndex
时打印速度慢的问题上 (GH 19764)修复了
GroupBy.nth()
和GroupBy.last()
在包含某些对象列时出现的性能回退问题 (GH 19283)改进了
Categorical.from_codes()
的性能 (GH 18501)
文档更改#
感谢所有参与 3 月 10 日 pandas 文档冲刺的贡献者。我们有来自全球 30 多个地区的约 500 名参与者。您应该会注意到许多 API 文档字符串 已得到了极大的改进。
同时进行的贡献过多,无法为每项改进都包含一个发布说明,但这次 GitHub 搜索 应该能让您了解有多少文档字符串得到了改进。
特别感谢 Marc Garcia 组织了此次冲刺。欲了解更多信息,请阅读回顾冲刺的 NumFOCUS 博客文章。
错误修复#
分类数据#
警告
pandas 0.21 版本引入了一类与
CategoricalDtype
相关的错误,影响了诸如merge
、concat
和索引等操作的正确性,尤其是在比较多个类别相同但顺序不同的无序Categorical
数组时。我们强烈建议在执行这些操作之前升级或手动对齐您的类别。Categorical.equals
中的错误,当比较两个类别相同但顺序不同的无序Categorical
数组时返回错误结果 (GH 16603)pandas.api.types.union_categoricals()
中的错误,当处理类别顺序不同的无序 categoricals 时返回错误结果。这影响了包含 Categorical data 的pandas.concat()
(GH 19096)。pandas.merge()
中的错误,当在类别相同但顺序不同的无序Categorical
上进行连接时返回错误结果 (GH 19551)CategoricalIndex.get_indexer()
中的错误,当target
是一个类别与self
相同但顺序不同的无序Categorical
时返回错误结果 (GH 19551)Index.astype()
中的错误,在使用 categorical dtype 时,对于所有类型的索引,结果索引都未被转换为CategoricalIndex
(GH 18630)Series.astype()
和Categorical.astype()
中的错误,现有 categorical data 未被更新 (GH 10696, GH 18593)Series.str.split()
中的错误,在使用expand=True
参数时,对于空字符串错误地引发了 IndexError (GH 20002)。Index
构造函数中的错误,在使用dtype=CategoricalDtype(...)
参数时,categories
和ordered
未被保留 (GH 19032)Series
构造函数中的错误,在使用标量和dtype=CategoricalDtype(...)
参数时,categories
和ordered
未被保留 (GH 19565)Categorical.__iter__
中的错误,未转换为 Python 类型 (GH 19909)pandas.factorize()
中的错误,返回了uniques
的唯一代码。现在此方法返回一个与输入具有相同 dtype 的Categorical
(GH 19721)pandas.factorize()
中的错误,在uniques
返回值中包含了缺失值的项 (GH 19721)Series.take()
中的错误,在 categorical data 中将indices
参数中的-1
解释为缺失值标记,而不是 Series 的最后一个元素 (GH 20664)
日期/时间类#
Series.__sub__()
中的错误,将非纳秒级np.datetime64
对象从Series
中减去时得到错误结果 (GH 7996)DatetimeIndex
,TimedeltaIndex
中的错误,加减零维整数数组时得到错误结果 (GH 19012)DatetimeIndex
和TimedeltaIndex
中的错误,在加减类似数组的DateOffset
对象时,要么引发错误 (np.array
,pd.Index
),要么广播不正确 (pd.Series
) (GH 18849)Series.__add__()
中的错误,将 dtype 为timedelta64[ns]
的 Series 添加到时区感知的DatetimeIndex
时,错误地丢弃了时区信息 (GH 13905)将
Period
对象添加到datetime
或Timestamp
对象时,现在将正确地引发TypeError
(GH 17983)Timestamp
中的错误,与Timestamp
对象数组进行比较时会导致RecursionError
(GH 15183)Series
floor-division 中的错误,对标量timedelta
执行操作时会引发异常 (GH 18846)DatetimeIndex
中的错误,其 repr 未显示一天结束时的高精度时间值(例如,23:59:59.999999999) (GH 19030).astype()
中的错误,转换为非纳秒级 timedelta 单位时会保持不正确的 dtype (GH 19176, GH 19223, GH 12425)Series.truncate()
中的错误,在使用单调PeriodIndex
时会引发TypeError
(GH 17717)pct_change()
中的错误,使用periods
和freq
参数时返回了不同长度的输出 (GH 7292)DatetimeIndex
与None
或datetime.date
对象比较中的错误,对于==
和!=
比较,本应分别返回全False
和全True
,却引发了TypeError
(GH 19301)Timestamp
和to_datetime()
中的错误,表示微超出范围时间戳的字符串被错误地向下取整,而不是引发OutOfBoundsDatetime
(GH 19382)Timestamp.floor()
DatetimeIndex.floor()
中的错误,未来和过去很远的时间戳未被正确取整 (GH 19206)to_datetime()
中的错误,在使用errors='coerce'
和utc=True
参数时,传入超出范围的日期时间会引发OutOfBoundsDatetime
,而不是解析为NaT
(GH 19612)DatetimeIndex
和TimedeltaIndex
加减法中的错误,返回对象的名称并非总是设置一致。 (GH 19744)DatetimeIndex
和TimedeltaIndex
加减法中的错误,与 numpy 数组进行操作时引发了TypeError
(GH 19847)DatetimeIndex
和TimedeltaIndex
中的错误,设置freq
属性未能完全支持 (GH 20678)
时间差#
Timedelta.__mul__()
中的错误,与NaT
相乘时返回了NaT
,而不是引发TypeError
(GH 19819)dtype 为
dtype='timedelta64[ns]'
的Series
中的错误,与TimedeltaIndex
相加或相减时结果被强制转换为dtype='int64'
(GH 17250)dtype 为
dtype='timedelta64[ns]'
的Series
中的错误,与TimedeltaIndex
相加或相减时可能返回名称不正确的Series
(GH 19043)Timedelta.__floordiv__()
和Timedelta.__rfloordiv__()
中的错误,错误地允许与许多不兼容的 numpy 对象相除 (GH 18846)将标量 timedelta-like 对象与
TimedeltaIndex
相除时执行了倒数操作的错误 (GH 19125)TimedeltaIndex
中的错误,与Series
相除时返回了TimedeltaIndex
而不是Series
(GH 19042)Timedelta.__add__()
,Timedelta.__sub__()
中的错误,与np.timedelta64
对象相加或相减时返回了另一个np.timedelta64
,而不是Timedelta
(GH 19738)Timedelta.__floordiv__()
,Timedelta.__rfloordiv__()
中的错误,与Tick
对象进行操作时会引发TypeError
,而不是返回 numeric value (GH 19738)Period.asfreq()
中的错误,靠近datetime(1, 1, 1)
的 period 可能被错误转换 (GH 19643, GH 19834)Timedelta.total_seconds()
中的错误,导致精度问题,例如Timedelta('30S').total_seconds()==30.000000000000004
(GH 19458)Timedelta.__rmod__()
中的错误,与numpy.timedelta64
进行操作时返回了timedelta64
对象,而不是Timedelta
(GH 19820)TimedeltaIndex
与TimedeltaIndex
相乘时,在 length mismatch 的情况下,现在将引发TypeError
而不是ValueError
(GH 19333)修复了使用
np.timedelta64
对象对TimedeltaIndex
进行索引时引发TypeError
的 bug (GH 20393)
时区#
修复了从包含 tz-naive 和 tz-aware 值的数组创建
Series
时,resultingSeries
的 dtype 是 tz-aware 而不是 object 的 bug (GH 16406)修复了 timezone-aware
DatetimeIndex
与NaT
比较时错误地引发TypeError
的 bug (GH 19276)修复了
DatetimeIndex.astype()
在 timezone aware dtypes 之间转换以及从 timezone aware 转换为 naive 时存在的 bug (GH 18951)修复了
DatetimeIndex
比较时,当尝试比较 timezone-aware 和 timezone-naive datetime-like 对象时未能引发TypeError
的 bug (GH 18162)修复了在
Series
构造函数中使用datetime64[ns, tz]
dtype 时,对 naive datetime 字符串进行本地化时存在的 bug (GH 174151)Timestamp.replace()
现在将优雅地处理夏令时转换 (GH 18319)修复了 tz-aware
DatetimeIndex
与TimedeltaIndex
或dtype='timedelta64[ns]'
数组进行加/减运算时结果不正确的 bug (GH 17558)修复了
DatetimeIndex.insert()
将NaT
插入 timezone-aware index 时错误地引发异常的 bug (GH 16357)修复了
DataFrame
构造函数中,tz-aware Datetimeindex 和给定的列名会导致一个空的DataFrame
的 bug (GH 19157)修复了
Timestamp.tz_localize()
在本地化接近最小或最大有效值的 timestamp 时可能溢出并返回具有不正确纳秒值的 timestamp 的 bug (GH 12677)修复了迭代使用固定时区偏移进行本地化,并将纳秒精度四舍五入到微秒的
DatetimeIndex
时存在的 bug (GH 19603)修复了
DataFrame.diff()
在处理 tz-aware 值时引发IndexError
的 bug (GH 18578)修复了在对包含 timezone-aware 值的单列调用
Dataframe.dropna()
后,Dataframe.count()
引发ValueError
的 bug (GH 13407)
偏移量#
修复了
WeekOfMonth
和Week
在加减运算时滚动不正确的 bug (GH 18510, GH 18672, GH 18864)修复了
WeekOfMonth
和LastWeekOfMonth
的构造函数中默认关键字参数引发ValueError
的 bug (GH 19142)修复了
FY5253Quarter
和LastWeekOfMonth
的回滚和前滚行为与加减行为不一致的 bug (GH 18854)修复了
FY5253
在对年末但未归一化到午夜的日期进行datetime
加减运算时增量不正确的 bug (GH 18854)修复了
FY5253
中 date offsets 在算术运算中可能错误地引发AssertionError
的 bug (GH 14774)
数值#
修复了
Series
构造函数在接收 int 或 float 列表并指定dtype=str
、dtype='str'
或dtype='U'
时未能将数据元素转换为字符串的 bug (GH 16605)修复了
Index
的乘法和除法方法在与Series
进行运算时返回Index
对象而不是Series
对象的 bug (GH 19042)修复了
DataFrame
构造函数中,包含非常大正数或负数的数据导致OverflowError
的 bug (GH 18584)修复了
Index
构造函数在使用dtype='uint64'
时,未将 int-like float 强制转换为UInt64Index
的 bug (GH 18400)修复了
DataFrame
的 flex 算术运算 (例如df.add(other, fill_value=foo)
) 在fill_value
不是None
时,当 frame 或other
的长度为零时未能引发NotImplementedError
的 bug (GH 19522)修复了数值 dtype 的
Index
对象与 timedelta-like scalar 进行乘法和除法运算时返回TimedeltaIndex
而不是引发TypeError
的 bug (GH 19333)修复了当
fill_method
不是None
时,Series.pct_change()
和DataFrame.pct_change()
返回NaN
而不是 0 的 bug (GH 19873)
字符串#
修复了
Series.str.get()
在值包含字典且索引不在键中时引发KeyError
的 bug (GH 20671)
索引#
修复了
Index.drop()
在传入同时包含元组和非元组的列表时存在的 bug (GH 18304)修复了
DataFrame.drop()
、Panel.drop()
、Series.drop()
、Index.drop()
在从包含重复项的轴删除不存在的元素时未引发KeyError
的 bug (GH 19186)修复了索引 datetime-like
Index
时引发ValueError
而不是IndexError
的 bug (GH 18386)Index.to_series()
现在接受index
和name
关键字参数 (GH 18699)DatetimeIndex.to_series()
现在接受index
和name
关键字参数 (GH 18699)修复了从具有非唯一
Index
的Series
索引非标量值时会返回扁平化值的 bug (GH 17610)修复了使用只包含缺失键的迭代器进行索引时未引发错误 (GH 20748)
修复了当索引为整数 dtype 且不包含期望的键时,
.ix
在使用列表和标量键之间存在的不一致性 (GH 20753)修复了使用 2 维布尔 ndarray 对
DataFrame
进行索引时,__setitem__
中存在的 bug (GH 18582)修复了
str.extractall
在没有匹配项时返回空Index
而不是适当的MultiIndex
的 bug (GH 19034)修复了
IntervalIndex
在构建空数据或纯 NA 数据时,根据构建方法不同存在不一致性的 bug (GH 18421)修复了
IntervalIndex.symmetric_difference()
与非IntervalIndex
进行对称差分时未引发异常的 bug (GH 18475)修复了
IntervalIndex
中返回空IntervalIndex
的集合运算具有错误 dtype 的 bug (GH 19101)修复了
DataFrame.drop_duplicates()
在传入DataFrame
中不存在的列时未引发KeyError
的 bug (GH 19726)修复了
Index
子类的构造函数忽略意外关键字参数的 bug (GH 19348)修复了
Index.difference()
计算Index
与自身差异时存在的 bug (GH 20040)修复了在值中间存在整行 NaNs 时,
DataFrame.first_valid_index()
和DataFrame.last_valid_index()
中的 bug (GH 20499)修复了
IntervalIndex
在处理重叠或非单调的uint64
数据时不支持某些索引操作的 bug (GH 20636)修复了
Series.is_unique
中,如果 Series 包含定义了__ne__
的对象,则在 stderr 中显示额外输出的 bug (GH 20661)修复了
.loc
使用单元素 list-like 进行赋值时错误地赋值为列表的 bug (GH 19474)修复了对具有单调递减
DatetimeIndex
的Series/DataFrame
进行部分字符串索引时存在的 bug (GH 19362)修复了对具有重复
Index
的DataFrame
执行原地操作时存在的 bug (GH 17105)修复了
IntervalIndex.get_loc()
和IntervalIndex.get_indexer()
在用于包含单个间隔的IntervalIndex
时存在的 bug (GH 17284, GH 20921)修复了
.loc
使用uint64
indexer 时存在的 bug (GH 20722)
多级索引#
修复了
MultiIndex.__contains__()
中,非元组键即使已被丢弃仍返回True
的 bug (GH 19027)修复了
MultiIndex.set_labels()
中,如果level
参数不是 0 或类似 [0, 1, …] 的列表,会导致新 labels 被 cast (并可能被 clipping) 的 bug (GH 19057)修复了
MultiIndex.get_level_values()
在整数 levels 且包含缺失值时返回无效索引的 bug (GH 17924)修复了对空
MultiIndex
调用MultiIndex.unique()
时存在的 bug (GH 20568)修复了
MultiIndex.unique()
不保留 level 名称的 bug (GH 20570)修复了
MultiIndex.remove_unused_levels()
会填充 nan 值的 bug (GH 18417)修复了
MultiIndex.from_tuples()
在 python3 中无法处理 zipped tuples 的 bug (GH 18434)修复了
MultiIndex.get_loc()
无法自动在 float 和 int 之间转换值的 bug (GH 18818, GH 15994)修复了
MultiIndex.get_loc()
将布尔值转换为整数 labels 的 bug (GH 19086)修复了
MultiIndex.get_loc()
无法定位包含NaN
的键的 bug (GH 18485)修复了在大型
MultiIndex
中,如果 levels 具有不同 dtypes 则MultiIndex.get_loc()
会失败的 bug (GH 18520)修复了索引时,只包含 numpy 数组的嵌套 indexers 处理不正确的 bug (GH 19686)
IO#
read_html()
现在在解析失败后,在尝试使用新的解析器之前,会重置可查找 (seekable) IO 对象的位置。如果解析器出错且对象不可查找,则会引发一个信息性错误,建议使用不同的解析器 (GH 17975)DataFrame.to_html()
现在提供了为开头的<table>
标签添加 id 的选项 (GH 8496)修复了在 Python 2 中,
read_msgpack()
传入不存在的文件时存在的 bug (GH 15296)修复了
read_csv()
中,包含重复列的MultiIndex
未被适当地 mangled 的 bug (GH 18062)修复了
read_csv()
中,当keep_default_na=False
且na_values
为字典时,缺失值未被正确处理的 bug (GH 19227)修复了
read_csv()
在 32 位大端架构上导致堆损坏的 bug (GH 20785)修复了
read_sas()
中,包含 0 个变量的文件错误地引发AttributeError
的 bug。现在会引发EmptyDataError
(GH 18184)修复了
DataFrame.to_latex()
中,旨在作为不可见占位符的花括号对被转义的 bug (GH 18667)修复了
DataFrame.to_latex()
中,MultiIndex
中的NaN
会导致IndexError
或错误输出的 bug (GH 14249)修复了
DataFrame.to_latex()
中,非字符串 index-level 名称会导致AttributeError
的 bug (GH 19981)修复了
DataFrame.to_latex()
中,index name 和index_names=False
选项的组合会导致错误输出的 bug (GH 18326)在
DataFrame.to_latex()
中,当MultiIndex
的名称为空字符串时,会导致输出不正确的问题 (GH 18669)在
DataFrame.to_latex()
中,缺少空格字符导致转义错误并在某些情况下产生无效的 latex 输出的问题 (GH 20859)在
read_json()
中,较大的数值导致OverflowError
的问题 (GH 18842)在
DataFrame.to_parquet()
中,如果写入目标是 S3,则会引发异常的问题 (GH 19134)Interval
现在在DataFrame.to_excel()
中支持所有 Excel 文件类型 (GH 19242)Timedelta
现在在DataFrame.to_excel()
中支持所有 Excel 文件类型 (GH 19242, GH 9155, GH 19900)在
pandas.io.stata.StataReader.value_labels()
中,当处理非常旧的文件时引发AttributeError
的问题。现在返回一个空字典 (GH 19417)在
read_pickle()
中,反序列化使用早于 0.20 版本 pandas 创建的包含TimedeltaIndex
或Float64Index
的对象时出现问题 (GH 19939)在
pandas.io.json.json_normalize()
中,如果任何子记录的值为 NoneType,则子记录无法正确标准化的问题 (GH 20030)在
read_csv()
中,当传递字符串时,usecols
参数未能正确引发错误的问题。 (GH 20529)在
HDFStore.keys()
中,读取带有软链接的文件时导致异常的问题 (GH 20523)在
HDFStore.select_column()
中,键无效时引发AttributeError
而不是KeyError
的问题 (GH 17912)
绘图#
尝试绘图但未安装 matplotlib 时提供更好的错误消息 (GH 19810)。
DataFrame.plot()
现在在x
或y
参数格式不正确时引发ValueError
(GH 18671)在
DataFrame.plot()
中,当x
和y
参数以位置形式给出时,导致折线图、条形图和面积图引用列不正确的问题 (GH 20056)使用
datetime.time()
和小数秒格式化刻度标签时出现问题 (GH 18478)。Series.plot.kde()
在文档字符串中暴露了参数ind
和bw_method
(GH 18461)。参数ind
现在也可以是整数(样本点数)。DataFrame.plot()
现在支持y
参数使用多个列 (GH 19699)
GroupBy/resample/rolling#
按单个列分组并使用
list
或tuple
等类进行聚合时出现问题 (GH 18079)修复了
DataFrame.groupby()
中的回归问题,该问题在调用时使用不在索引中的元组键不会发出错误 (GH 18798)在
DataFrame.resample()
中,静默忽略label
、closed
和convention
的不支持(或拼写错误)选项的问题 (GH 19303)在
DataFrame.groupby()
中,元组被解释为键列表而不是键的问题 (GH 17979, GH 18249)在
DataFrame.groupby()
中,通过first
/last
/min
/max
聚合导致时间戳丢失精度的问题 (GH 19526)在
DataFrame.transform()
中,特定的聚合函数被错误地转换为与分组数据 dtype 匹配的问题 (GH 19200)在
DataFrame.groupby()
中,传递on=
kwarg 然后使用.apply()
时出现问题 (GH 17813)在
DataFrame.resample().aggregate
中,聚合不存在的列时未能引发KeyError
的问题 (GH 16766, GH 19566)在
DataFrameGroupBy.cumsum()
和DataFrameGroupBy.cumprod()
中,传递skipna
时出现问题 (GH 19806)在
DataFrame.resample()
中,丢失时区信息的问题 (GH 13238)在
DataFrame.groupby()
中,使用np.all
和np.any
进行转换时引发ValueError
的问题 (GH 20653)在
DataFrame.resample()
中,ffill
,bfill
,pad
,backfill
,fillna
,interpolate
和asfreq
忽略loffset
的问题。 (GH 20744)在
DataFrame.groupby()
中,应用具有混合数据类型且用户提供的函数可能在分组列上失败的函数时出现问题 (GH 20949)在
DataFrameGroupBy.rolling().apply()
中,对相关DataFrameGroupBy
对象执行的操作可能会影响分组项是否包含在结果中的问题 (GH 14013)
稀疏数据#
重塑#
在
DataFrame.merge()
中,通过名称引用CategoricalIndex
时,by
kwarg 会导致KeyError
的问题 (GH 20777)在
DataFrame.stack()
中,在 Python 3 下尝试排序混合类型级别时失败的问题 (GH 18310)在
DataFrame.unstack()
中,如果columns
是具有未使用级别的MultiIndex
,则将 int 转换为 float 的问题 (GH 17845)在
DataFrame.unstack()
中,如果index
是具有未堆叠级别上未使用标签的MultiIndex
,则会引发错误的问题 (GH 18562)禁用了构造
Series
的行为,在之前 len(index) > len(data) = 1 时会广播数据项,现在会引发ValueError
(GH 18819)抑制了从包含标量值的
dict
构造DataFrame
时,当传入的索引中不包含相应键时引发的错误 (GH 18600)修复了使用轴、无数据且
dtype=int
初始化DataFrame
时的 dtype (从object
更改为float64
) (GH 19646)在
Series.rank()
中,包含NaT
的Series
会就地修改Series
的问题 (GH 18521)在
DataFrame.pivot_table()
中,当aggfunc
参数类型为字符串时失败的问题。现在的行为与其他方法如agg
和apply
一致 (GH 18713)在
DataFrame.merge()
中,使用Index
对象作为向量进行合并时引发异常的问题 (GH 19038)在
DataFrame.stack()
,DataFrame.unstack()
,Series.unstack()
中,没有返回子类的问题 (GH 15563)时区比较时出现问题,表现为在
.concat()
中将索引转换为 UTC (GH 18523)在
concat()
中,连接稀疏和密集 series 时仅返回SparseDataFrame
的问题。应该返回DataFrame
。 (GH 18914, GH 18686, 和 GH 16874)当没有共同的合并键时,
DataFrame.merge()
提供更清晰的错误消息 (GH 19427)在
DataFrame.join()
中,当与多个 DataFrame 连接且其中一些具有非唯一索引时,执行outer
而不是left
连接的问题 (GH 19624)Series.rename()
现在接受axis
作为 kwarg (GH 18589)Series
和Index
之间的比较会返回名称不正确(忽略Index
的 name 属性)的Series
(GH 19582)在
qcut()
中,当存在NaT
的 datetime 和 timedelta 数据引发ValueError
的问题 (GH 19768)在
DataFrame.iterrows()
中,会将不符合 ISO8601 规范的字符串推断为 datetime 的问题 (GH 19671)使用
Categorical
构造Series
时,给定不同长度的索引未能引发ValueError
的问题 (GH 19342)在
DataFrame.astype()
中,转换为 categorical 或字典 dtype 时列元数据丢失的问题 (GH 19920)在
get_dummies()
和select_dtypes()
中,重复列名导致行为不正确的问题 (GH 20848)在
concat()
中,连接 TZ-aware dataframes 和全 NaT dataframes 时引发错误的问题 (GH 12396)
其他#
尝试在支持
numexpr
的查询中使用 Python 关键字作为标识符时提供更清晰的错误消息 (GH 18221)在访问
pandas.get_option()
时,在某些情况下查找不存在的选项键时引发KeyError
而不是OptionError
的问题 (GH 19789)在
testing.assert_series_equal()
和testing.assert_frame_equal()
中,Series 或 DataFrame 具有不同 Unicode 数据时出现问题 (GH 20503)
贡献者#
共有 328 人为此版本贡献了补丁。名字旁边带有“+”的人是第一次贡献补丁。
Aaron Critchley
AbdealiJK +
Adam Hooper +
Albert Villanova del Moral
Alejandro Giacometti +
Alejandro Hohmann +
Alex Rychyk
Alexander Buchkovsky
Alexander Lenail +
Alexander Michael Schade
Aly Sivji +
Andreas Költringer +
Andrew
Andrew Bui +
András Novoszáth +
Andy Craze +
Andy R. Terrel
Anh Le +
Anil Kumar Pallekonda +
Antoine Pitrou +
Antonio Linde +
Antonio Molina +
Antonio Quinonez +
Armin Varshokar +
Artem Bogachev +
Avi Sen +
Azeez Oluwafemi +
Ben Auffarth +
Bernhard Thiel +
Bhavesh Poddar +
BielStela +
Blair +
Bob Haffner
Brett Naul +
Brock Mendel
Bryce Guinta +
Carlos Eduardo Moreira dos Santos +
Carlos García Márquez +
Carol Willing
Cheuk Ting Ho +
Chitrank Dixit +
Chris
Chris Burr +
Chris Catalfo +
Chris Mazzullo
Christian Chwala +
Cihan Ceyhan +
Clemens Brunner
Colin +
Cornelius Riemenschneider
Crystal Gong +
DaanVanHauwermeiren
Dan Dixey +
Daniel Frank +
Daniel Garrido +
Daniel Sakuma +
DataOmbudsman +
Dave Hirschfeld
Dave Lewis +
David Adrián Cañones Castellano +
David Arcos +
David C Hall +
David Fischer
David Hoese +
David Lutz +
David Polo +
David Stansby
Dennis Kamau +
Dillon Niederhut
Dimitri +
Dr. Irv
Dror Atariah
Eric Chea +
Eric Kisslinger
Eric O. LEBIGOT (EOL) +
FAN-GOD +
Fabian Retkowski +
Fer Sar +
Gabriel de Maeztu +
Gianpaolo Macario +
Giftlin Rajaiah
Gilberto Olimpio +
Gina +
Gjelt +
Graham Inggs +
Grant Roch
Grant Smith +
Grzegorz Konefał +
Guilherme Beltramini
HagaiHargil +
Hamish Pitkeathly +
Hammad Mashkoor +
Hannah Ferchland +
Hans
Haochen Wu +
Hissashi Rocha +
Iain Barr +
Ibrahim Sharaf ElDen +
Ignasi Fosch +
Igor Conrado Alves de Lima +
Igor Shelvinskyi +
Imanflow +
Ingolf Becker
Israel Saeta Pérez
Iva Koevska +
Jakub Nowacki +
Jan F-F +
Jan Koch +
Jan Werkmann
Janelle Zoutkamp +
Jason Bandlow +
Jaume Bonet +
Jay Alammar +
Jeff Reback
JennaVergeynst
Jimmy Woo +
Jing Qiang Goh +
Joachim Wagner +
Joan Martin Miralles +
Joel Nothman
Joeun Park +
John Cant +
Johnny Metz +
Jon Mease
Jonas Schulze +
Jongwony +
Jordi Contestí +
Joris Van den Bossche
José F. R. Fonseca +
Jovixe +
Julio Martinez +
Jörg Döpfert
KOBAYASHI Ittoku +
Kate Surta +
Kenneth +
Kevin Kuhl
Kevin Sheppard
Krzysztof Chomski
Ksenia +
Ksenia Bobrova +
Kunal Gosar +
Kurtis Kerstein +
Kyle Barron +
Laksh Arora +
Laurens Geffert +
Leif Walsh
Liam Marshall +
Liam3851 +
Licht Takeuchi
Liudmila +
Ludovico Russo +
Mabel Villalba +
Manan Pal Singh +
Manraj Singh
Marc +
Marc Garcia
Marco Hemken +
Maria del Mar Bibiloni +
Mario Corchero +
Mark Woodbridge +
Martin Journois +
Mason Gallo +
Matias Heikkilä +
Matt Braymer-Hayes
Matt Kirk +
Matt Maybeno +
Matthew Kirk +
Matthew Rocklin +
Matthew Roeschke
Matthias Bussonnier +
Max Mikhaylov +
Maxim Veksler +
Maximilian Roos
Maximiliano Greco +
Michael Penkov
Michael Röttger +
Michael Selik +
Michael Waskom
Mie~~~
Mike Kutzma +
Ming Li +
Mitar +
Mitch Negus +
Montana Low +
Moritz Münst +
Mortada Mehyar
Myles Braithwaite +
Nate Yoder
Nicholas Ursa +
Nick Chmura
Nikos Karagiannakis +
Nipun Sadvilkar +
Nis Martensen +
Noah +
Noémi Éltető +
Olivier Bilodeau +
Ondrej Kokes +
Onno Eberhard +
Paul Ganssle +
Paul Mannino +
Paul Reidy
Paulo Roberto de Oliveira Castro +
Pepe Flores +
Peter Hoffmann
Phil Ngo +
Pietro Battiston
Pranav Suri +
Priyanka Ojha +
Pulkit Maloo +
README Bot +
Ray Bell +
Riccardo Magliocchetti +
Ridhwan Luthra +
Robert Meyer
Robin
Robin Kiplang’at +
Rohan Pandit +
Rok Mihevc +
Rouz Azari
Ryszard T. Kaleta +
Sam Cohan
Sam Foo
Samir Musali +
Samuel Sinayoko +
Sangwoong Yoon
SarahJessica +
Sharad Vijalapuram +
Shubham Chaudhary +
SiYoungOh +
Sietse Brouwer
Simone Basso +
Stefania Delprete +
Stefano Cianciulli +
Stephen Childs +
StephenVoland +
Stijn Van Hoey +
Sven
Talitha Pumar +
Tarbo Fukazawa +
Ted Petrou +
Thomas A Caswell
Tim Hoffmann +
Tim Swast
Tom Augspurger
Tommy +
Tulio Casagrande +
Tushar Gupta +
Tushar Mittal +
Upkar Lidder +
Victor Villas +
Vince W +
Vinícius Figueiredo +
Vipin Kumar +
WBare
Wenhuan +
Wes Turner
William Ayd
Wilson Lin +
Xbar
Yaroslav Halchenko
Yee Mey
Yeongseon Choe +
Yian +
Yimeng Zhang
ZhuBaohe +
Zihao Zhao +
adatasetaday +
akielbowicz +
akosel +
alinde1 +
amuta +
bolkedebruin
cbertinato
cgohlke
charlie0389 +
chris-b1
csfarkas +
dajcs +
deflatSOCO +
derestle-htwg
discort
dmanikowski-reef +
donK23 +
elrubio +
fivemok +
fjdiod
fjetter +
froessler +
gabrielclow
gfyoung
ghasemnaddaf
h-vetinari +
himanshu awasthi +
ignamv +
jayfoad +
jazzmuesli +
jbrockmendel
jen w +
jjames34 +
joaoavf +
joders +
jschendel
juan huguet +
l736x +
luzpaz +
mdeboc +
miguelmorin +
miker985
miquelcamprodon +
orereta +
ottiP +
peterpanmj +
rafarui +
raph-m +
readyready15728 +
rmihael +
samghelms +
scriptomation +
sfoo +
stefansimik +
stonebig
tmnhat2001 +
tomneep +
topper-123
tv3141 +
verakai +
xpvpc +
zhanghui +