Pandas-2-2-中文文档-六十一-
Pandas 2.2 中文文档(六十一)
版本 0.10.1(2013 年 1 月 22 日)
这是从 0.10.0 的一个小版本,包括新功能、增强功能和错误修复。特别是,由 Jeff Reback 贡献的新的 HDFStore 功能。
与接受inplace
选项的函数发生的不良 API 中断已被撤销并添加了弃用警告。
API 更改
-
接受
inplace
选项的函数将像以前一样返回调用对象。已添加弃用消息 -
Groupby 聚合 Max/Min 不再排除非数字数据(GH 2700)
-
对空 DataFrame 进行重新采样现在返回空 DataFrame 而不是引发异常(GH 2640)
-
文件读取器现在在明确指定的整数列中发现 NA 值时将引发异常,而不是将列转换为浮点数(GH 2631)
-
DatetimeIndex.unique 现在返回具有相同名称和的 DatetimeIndex
-
时区而不是数组(GH 2563)
新功能
- MySQL 数据库支持(Dan Allan 的贡献)
HDFStore
您可能需要升级现有数据文件。请访问主文档中的兼容性部分。
您可以指定(并索引)您希望能够在表上执行查询的某些列,通过将列表传递给data_columns
In [1]: store = pd.HDFStore("store.h5")
In [2]: df = pd.DataFrame(
...: np.random.randn(8, 3),
...: index=pd.date_range("1/1/2000", periods=8),
...: columns=["A", "B", "C"],
...: )
...:
In [3]: df["string"] = "foo"
In [4]: df.loc[df.index[4:6], "string"] = np.nan
In [5]: df.loc[df.index[7:9], "string"] = "bar"
In [6]: df["string2"] = "cool"
In [7]: df
Out[7]:
A B C string string2
2000-01-01 0.469112 -0.282863 -1.509059 foo cool
2000-01-02 -1.135632 1.212112 -0.173215 foo cool
2000-01-03 0.119209 -1.044236 -0.861849 foo cool
2000-01-04 -2.104569 -0.494929 1.071804 foo cool
2000-01-05 0.721555 -0.706771 -1.039575 NaN cool
2000-01-06 0.271860 -0.424972 0.567020 NaN cool
2000-01-07 0.276232 -1.087401 -0.673690 foo cool
2000-01-08 0.113648 -1.478427 0.524988 bar cool
# on-disk operations
In [8]: store.append("df", df, data_columns=["B", "C", "string", "string2"])
In [9]: store.select("df", "B>0 and string=='foo'")
Out[9]:
A B C string string2
2000-01-02 -1.135632 1.212112 -0.173215 foo cool
# this is in-memory version of this type of selection
In [10]: df[(df.B > 0) & (df.string == "foo")]
Out[10]:
A B C string string2
2000-01-02 -1.135632 1.212112 -0.173215 foo cool
检索可索引或数据列中的唯一值。
# note that this is deprecated as of 0.14.0
# can be replicated by: store.select_column('df','index').unique()
store.unique("df", "index")
store.unique("df", "string")
现在可以在数据列中存储datetime64
In [11]: df_mixed = df.copy()
In [12]: df_mixed["datetime64"] = pd.Timestamp("20010102")
In [13]: df_mixed.loc[df_mixed.index[3:4], ["A", "B"]] = np.nan
In [14]: store.append("df_mixed", df_mixed)
In [15]: df_mixed1 = store.select("df_mixed")
In [16]: df_mixed1
Out[16]:
A B ... string2 datetime64
2000-01-01 0.469112 -0.282863 ... cool 1970-01-01 00:00:00.978393600
2000-01-02 -1.135632 1.212112 ... cool 1970-01-01 00:00:00.978393600
2000-01-03 0.119209 -1.044236 ... cool 1970-01-01 00:00:00.978393600
2000-01-04 NaN NaN ... cool 1970-01-01 00:00:00.978393600
2000-01-05 0.721555 -0.706771 ... cool 1970-01-01 00:00:00.978393600
2000-01-06 0.271860 -0.424972 ... cool 1970-01-01 00:00:00.978393600
2000-01-07 0.276232 -1.087401 ... cool 1970-01-01 00:00:00.978393600
2000-01-08 0.113648 -1.478427 ... cool 1970-01-01 00:00:00.978393600
[8 rows x 6 columns]
In [17]: df_mixed1.dtypes.value_counts()
Out[17]:
float64 3
object 2
datetime64[ns] 1
Name: count, dtype: int64
您可以将columns
关键字传递给 select 以过滤返回列的列表,这相当于传递一个Term('columns',list_of_columns_to_filter)
In [18]: store.select("df", columns=["A", "B"])
Out[18]:
A B
2000-01-01 0.469112 -0.282863
2000-01-02 -1.135632 1.212112
2000-01-03 0.119209 -1.044236
2000-01-04 -2.104569 -0.494929
2000-01-05 0.721555 -0.706771
2000-01-06 0.271860 -0.424972
2000-01-07 0.276232 -1.087401
2000-01-08 0.113648 -1.478427
HDFStore
现在在追加表时序列化 MultiIndex 数据帧。
In [19]: index = pd.MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'],
....: ['one', 'two', 'three']],
....: labels=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3],
....: [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
....: names=['foo', 'bar'])
....:
In [20]: df = pd.DataFrame(np.random.randn(10, 3), index=index,
....: columns=['A', 'B', 'C'])
....:
In [21]: df
Out[21]:
A B C
foo bar
foo one -0.116619 0.295575 -1.047704
two 1.640556 1.905836 2.772115
three 0.088787 -1.144197 -0.633372
bar one 0.925372 -0.006438 -0.820408
two -0.600874 -1.039266 0.824758
baz two -0.824095 -0.337730 -0.927764
three -0.840123 0.248505 -0.109250
qux one 0.431977 -0.460710 0.336505
two -3.207595 -1.535854 0.409769
three -0.673145 -0.741113 -0.110891
In [22]: store.append('mi', df)
In [23]: store.select('mi')
Out[23]:
A B C
foo bar
foo one -0.116619 0.295575 -1.047704
two 1.640556 1.905836 2.772115
three 0.088787 -1.144197 -0.633372
bar one 0.925372 -0.006438 -0.820408
two -0.600874 -1.039266 0.824758
baz two -0.824095 -0.337730 -0.927764
three -0.840123 0.248505 -0.109250
qux one 0.431977 -0.460710 0.336505
two -3.207595 -1.535854 0.409769
three -0.673145 -0.741113 -0.110891
# the levels are automatically included as data columns
In [24]: store.select('mi', "foo='bar'")
Out[24]:
A B C
foo bar
bar one 0.925372 -0.006438 -0.820408
two -0.600874 -1.039266 0.824758
通过append_to_multiple
进行多表创建和通过select_as_multiple
进行选择可以创建/选择多个表并返回合并结果,通过在选择器表上使用where
。
In [19]: df_mt = pd.DataFrame(
....: np.random.randn(8, 6),
....: index=pd.date_range("1/1/2000", periods=8),
....: columns=["A", "B", "C", "D", "E", "F"],
....: )
....:
In [20]: df_mt["foo"] = "bar"
# you can also create the tables individually
In [21]: store.append_to_multiple(
....: {"df1_mt": ["A", "B"], "df2_mt": None}, df_mt, selector="df1_mt"
....: )
....:
In [22]: store
Out[22]:
<class 'pandas.io.pytables.HDFStore'>
File path: store.h5
# individual tables were created
In [23]: store.select("df1_mt")
Out[23]:
A B
2000-01-01 0.404705 0.577046
2000-01-02 -1.344312 0.844885
2000-01-03 0.357021 -0.674600
2000-01-04 0.276662 -0.472035
2000-01-05 0.895717 0.805244
2000-01-06 -1.170299 -0.226169
2000-01-07 -0.076467 -1.187678
2000-01-08 1.024180 0.569605
In [24]: store.select("df2_mt")
Out[24]:
C D E F foo
2000-01-01 -1.715002 -1.039268 -0.370647 -1.157892 bar
2000-01-02 1.075770 -0.109050 1.643563 -1.469388 bar
2000-01-03 -1.776904 -0.968914 -1.294524 0.413738 bar
2000-01-04 -0.013960 -0.362543 -0.006154 -0.923061 bar
2000-01-05 -1.206412 2.565646 1.431256 1.340309 bar
2000-01-06 0.410835 0.813850 0.132003 -0.827317 bar
2000-01-07 1.130127 -1.436737 -1.413681 1.607920 bar
2000-01-08 0.875906 -2.211372 0.974466 -2.006747 bar
# as a multiple
In [25]: store.select_as_multiple(
....: ["df1_mt", "df2_mt"], where=["A>0", "B>0"], selector="df1_mt"
....: )
....:
Out[25]:
A B C D E F foo
2000-01-01 0.404705 0.577046 -1.715002 -1.039268 -0.370647 -1.157892 bar
2000-01-05 0.895717 0.805244 -1.206412 2.565646 1.431256 1.340309 bar
2000-01-08 1.024180 0.569605 0.875906 -2.211372 0.974466 -2.006747 bar
增强功能
-
HDFStore
现在可以读取本机 PyTables 表格格式表 -
您可以将
nan_rep = 'my_nan_rep'
传递给 append,以更改磁盘上的默认 nan 表示(将转换为/从np.nan
),默认为nan
。 -
您可以将
index
传递给append
。默认为True
。这将自动在表的可索引和数据列上创建索引 -
您可以将
chunksize=一个整数
传递给append
,以更改写入块大小(默认为 50000)。这将显着降低写入时的内存使用。 -
您可以将
expectedrows=一个整数
传递给第一个append
,以设置PyTables
预期的总行数。这将优化读/写性能。 -
Select
现在支持传递start
和stop
以提供选择空间限制。 -
大大改进了文件解析器的 ISO8601(例如,yyyy-mm-dd)日期解析能力(GH 2698)
-
允许
DataFrame.merge
处理对 64 位整数过大的组合大小(GH 2690) -
Series 现在具有一元否定(-series)和反转(~series)运算符(GH 2686)
-
DataFrame.plot 现在包括一个
logx
参数,以将 x 轴更改为对数刻度(GH 2327) -
Series 算术运算符现在可以处理常量和 ndarray 输入(GH 2574)
-
ExcelFile 现在接受一个
kind
参数来指定文件类型(GH 2613) -
Series.str 方法的更快实现(GH 2602)
错误修复
-
HDFStore
表现在可以正确存储float32
类型(但不能与float64
混合使用) -
修复了在指定请求段时修复 Google Analytics 前缀的问题(GH 2713).
-
重置 Google Analytics 令牌存储的功能,以便用户可以从不正确设置的客户端密钥中恢复(GH 2687).
-
修复了在传递 MultiIndex 时导致段错误的 groupby 错误(GH 2706)
-
修复了将具有 datetime64 值的 Series 传递到
to_datetime
时导致虚假输出值的错误(GH 2699) -
修复了在
pattern in HDFStore
表达式中传递无效正则表达式时的错误(GH 2694) -
在聚合布尔数据时大大提高了性能(GH 2692)
-
当给定布尔掩码键和一系列新值时,Series setitem 现在将传入的值与原始 Series 对齐(GH 2686)
-
修复了在具有非常大数量的组合值的 MultiIndex 级别上执行计数排序时导致 MemoryError 的错误(GH 2684)
-
修复了当索引是具有固定偏移时区的 DatetimeIndex 时绘图失败的错误(GH 2683)
-
当偏移超过 5 个工作日且起始日期为周末时,修正了工作日减法逻辑(GH 2680)
-
修复了当文件的列数多于数据时 C 文件解析器的行为(GH 2668)
-
修复了文件读取器错误,当存在隐式列和指定的
usecols
值时,列与数据不对齐 -
具有数值或日期时间索引的数据框现在在绘图之前进行排序(GH 2609)
-
当传递列、索引但是空记录时,修复了 DataFrame.from_records 的错误(GH 2633)
-
当 dtype 为 datetime64 时,Series 操作的若干错误已经修复(GH 2689, GH 2629, GH 2626)
请查看完整的发布说明或 GitHub 上的问题跟踪器以获取完整列表。
贡献者
总共有 17 人为此版本贡献了补丁。名字后面有“+”符号的人第一次贡献了补丁。
-
Andy Hayden +
-
Anton I. Sipos +
-
Chang She
-
Christopher Whelan
-
Damien Garaud +
-
Dan Allan +
-
Dieter Vandenbussche
-
Garrett Drapala +
-
Jay Parlar +
-
Thouis(Ray)Jones +
-
Vincent Arel-Bundock +
-
Wes McKinney
-
elpres
-
herrfz +
-
jreback
-
svaksha +
-
y-p
API 变更
-
以前采用
inplace
选项的函数返回调用对象。已添加了一条弃用消息 -
Groupby 聚合 Max/Min 不再排除非数字数据(GH 2700)
-
对空 DataFrame 进行重新采样现在会返回一个空 DataFrame,而不是引发异常(GH 2640)
-
当在显式指定的整数列中找到 NA 值时,文件读取器现在会引发异常,而不是将列转换为浮点数(GH 2631)
-
DatetimeIndex.unique 现在返回一个具有相同名称的 DatetimeIndex 和
-
时区而不是数组(GH 2563)
新功能
- MySQL 支持数据库(Dan Allan 的贡献)
HDFStore
您可能需要升级现有的数据文件。请访问主文档中的兼容性部分。
您可以指定(并索引)希望在表上执行查询的某些列,方法是将列表传递给data_columns
In [1]: store = pd.HDFStore("store.h5")
In [2]: df = pd.DataFrame(
...: np.random.randn(8, 3),
...: index=pd.date_range("1/1/2000", periods=8),
...: columns=["A", "B", "C"],
...: )
...:
In [3]: df["string"] = "foo"
In [4]: df.loc[df.index[4:6], "string"] = np.nan
In [5]: df.loc[df.index[7:9], "string"] = "bar"
In [6]: df["string2"] = "cool"
In [7]: df
Out[7]:
A B C string string2
2000-01-01 0.469112 -0.282863 -1.509059 foo cool
2000-01-02 -1.135632 1.212112 -0.173215 foo cool
2000-01-03 0.119209 -1.044236 -0.861849 foo cool
2000-01-04 -2.104569 -0.494929 1.071804 foo cool
2000-01-05 0.721555 -0.706771 -1.039575 NaN cool
2000-01-06 0.271860 -0.424972 0.567020 NaN cool
2000-01-07 0.276232 -1.087401 -0.673690 foo cool
2000-01-08 0.113648 -1.478427 0.524988 bar cool
# on-disk operations
In [8]: store.append("df", df, data_columns=["B", "C", "string", "string2"])
In [9]: store.select("df", "B>0 and string=='foo'")
Out[9]:
A B C string string2
2000-01-02 -1.135632 1.212112 -0.173215 foo cool
# this is in-memory version of this type of selection
In [10]: df[(df.B > 0) & (df.string == "foo")]
Out[10]:
A B C string string2
2000-01-02 -1.135632 1.212112 -0.173215 foo cool
在可索引或数据列中检索唯一值。
# note that this is deprecated as of 0.14.0
# can be replicated by: store.select_column('df','index').unique()
store.unique("df", "index")
store.unique("df", "string")
现在可以在数据列中存储datetime64
In [11]: df_mixed = df.copy()
In [12]: df_mixed["datetime64"] = pd.Timestamp("20010102")
In [13]: df_mixed.loc[df_mixed.index[3:4], ["A", "B"]] = np.nan
In [14]: store.append("df_mixed", df_mixed)
In [15]: df_mixed1 = store.select("df_mixed")
In [16]: df_mixed1
Out[16]:
A B ... string2 datetime64
2000-01-01 0.469112 -0.282863 ... cool 1970-01-01 00:00:00.978393600
2000-01-02 -1.135632 1.212112 ... cool 1970-01-01 00:00:00.978393600
2000-01-03 0.119209 -1.044236 ... cool 1970-01-01 00:00:00.978393600
2000-01-04 NaN NaN ... cool 1970-01-01 00:00:00.978393600
2000-01-05 0.721555 -0.706771 ... cool 1970-01-01 00:00:00.978393600
2000-01-06 0.271860 -0.424972 ... cool 1970-01-01 00:00:00.978393600
2000-01-07 0.276232 -1.087401 ... cool 1970-01-01 00:00:00.978393600
2000-01-08 0.113648 -1.478427 ... cool 1970-01-01 00:00:00.978393600
[8 rows x 6 columns]
In [17]: df_mixed1.dtypes.value_counts()
Out[17]:
float64 3
object 2
datetime64[ns] 1
Name: count, dtype: int64
您可以传递columns
关键字以选择过滤返回列的列表,这相当于传递Term('columns',list_of_columns_to_filter)
In [18]: store.select("df", columns=["A", "B"])
Out[18]:
A B
2000-01-01 0.469112 -0.282863
2000-01-02 -1.135632 1.212112
2000-01-03 0.119209 -1.044236
2000-01-04 -2.104569 -0.494929
2000-01-05 0.721555 -0.706771
2000-01-06 0.271860 -0.424972
2000-01-07 0.276232 -1.087401
2000-01-08 0.113648 -1.478427
在追加表时,HDFStore
现在序列化多重索引数据框。
In [19]: index = pd.MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'],
....: ['one', 'two', 'three']],
....: labels=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3],
....: [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
....: names=['foo', 'bar'])
....:
In [20]: df = pd.DataFrame(np.random.randn(10, 3), index=index,
....: columns=['A', 'B', 'C'])
....:
In [21]: df
Out[21]:
A B C
foo bar
foo one -0.116619 0.295575 -1.047704
two 1.640556 1.905836 2.772115
three 0.088787 -1.144197 -0.633372
bar one 0.925372 -0.006438 -0.820408
two -0.600874 -1.039266 0.824758
baz two -0.824095 -0.337730 -0.927764
three -0.840123 0.248505 -0.109250
qux one 0.431977 -0.460710 0.336505
two -3.207595 -1.535854 0.409769
three -0.673145 -0.741113 -0.110891
In [22]: store.append('mi', df)
In [23]: store.select('mi')
Out[23]:
A B C
foo bar
foo one -0.116619 0.295575 -1.047704
two 1.640556 1.905836 2.772115
three 0.088787 -1.144197 -0.633372
bar one 0.925372 -0.006438 -0.820408
two -0.600874 -1.039266 0.824758
baz two -0.824095 -0.337730 -0.927764
three -0.840123 0.248505 -0.109250
qux one 0.431977 -0.460710 0.336505
two -3.207595 -1.535854 0.409769
three -0.673145 -0.741113 -0.110891
# the levels are automatically included as data columns
In [24]: store.select('mi', "foo='bar'")
Out[24]:
A B C
foo bar
bar one 0.925372 -0.006438 -0.820408
two -0.600874 -1.039266 0.824758
通过append_to_multiple
进行多表创建,并通过select_as_multiple
进行选择,可以从多个表中创建/选择并返回一个合并的结果,在选择器表上使用where
。
In [19]: df_mt = pd.DataFrame(
....: np.random.randn(8, 6),
....: index=pd.date_range("1/1/2000", periods=8),
....: columns=["A", "B", "C", "D", "E", "F"],
....: )
....:
In [20]: df_mt["foo"] = "bar"
# you can also create the tables individually
In [21]: store.append_to_multiple(
....: {"df1_mt": ["A", "B"], "df2_mt": None}, df_mt, selector="df1_mt"
....: )
....:
In [22]: store
Out[22]:
<class 'pandas.io.pytables.HDFStore'>
File path: store.h5
# individual tables were created
In [23]: store.select("df1_mt")
Out[23]:
A B
2000-01-01 0.404705 0.577046
2000-01-02 -1.344312 0.844885
2000-01-03 0.357021 -0.674600
2000-01-04 0.276662 -0.472035
2000-01-05 0.895717 0.805244
2000-01-06 -1.170299 -0.226169
2000-01-07 -0.076467 -1.187678
2000-01-08 1.024180 0.569605
In [24]: store.select("df2_mt")
Out[24]:
C D E F foo
2000-01-01 -1.715002 -1.039268 -0.370647 -1.157892 bar
2000-01-02 1.075770 -0.109050 1.643563 -1.469388 bar
2000-01-03 -1.776904 -0.968914 -1.294524 0.413738 bar
2000-01-04 -0.013960 -0.362543 -0.006154 -0.923061 bar
2000-01-05 -1.206412 2.565646 1.431256 1.340309 bar
2000-01-06 0.410835 0.813850 0.132003 -0.827317 bar
2000-01-07 1.130127 -1.436737 -1.413681 1.607920 bar
2000-01-08 0.875906 -2.211372 0.974466 -2.006747 bar
# as a multiple
In [25]: store.select_as_multiple(
....: ["df1_mt", "df2_mt"], where=["A>0", "B>0"], selector="df1_mt"
....: )
....:
Out[25]:
A B C D E F foo
2000-01-01 0.404705 0.577046 -1.715002 -1.039268 -0.370647 -1.157892 bar
2000-01-05 0.895717 0.805244 -1.206412 2.565646 1.431256 1.340309 bar
2000-01-08 1.024180 0.569605 0.875906 -2.211372 0.974466 -2.006747 bar
增强
-
HDFStore
现在可以读取本地 PyTables 表格式的表格 -
您可以传递
nan_rep = 'my_nan_rep'
以更改磁盘上的默认 nan 表示(将其转换为/fromnp.nan
),默认为nan
。 -
您可以将
index
传递给append
。默认为True
。这将自动在表的索引和数据列上创建索引 -
您可以将
chunksize=一个整数
传递给append
,以更改写入块大小(默认为 50000)。这将显著降低写入时的内存使用。 -
您可以将
expectedrows=一个整数
传递给第一个append
,以设置PyTables
将期望的总行数。这将优化读/写性能。 -
Select
现在支持传递start
和stop
以提供选择空间限制的选择。 -
大大改进了 ISO8601(例如,yyyy-mm-dd)日期解析的文件解析器(GH 2698)
-
允许
DataFrame.merge
处理组合大小过大以至于超出 64 位整数范围的情况(GH 2690) -
Series 现在具有一元否定(-series)和反转(~series)运算符(GH 2686)
-
DataFrame.plot 现在包括一个
logx
参数,以将 x 轴更改为对数刻度(GH 2327) -
Series 算术运算符现在可以处理常量和 ndarray 输入(GH 2574)
-
ExcelFile 现在接受一个
kind
参数来指定文件类型(GH 2613) -
Series.str 方法的更快实现(GH 2602)
错误修复
-
HDFStore
表现在可以正确存储float32
类型(但不能与float64
混合) -
修复了指定请求段时的 Google Analytics 前缀错误(GH 2713)。
-
重置 Google Analytics 令牌存储的功能,以便用户可以从不正确设置的客户端密钥中恢复(GH 2687)。
-
修复了传入 MultiIndex 时导致分组错误的 bug(GH 2706)
-
修复了将包含 datetime64 值的 Series 传递给
to_datetime
时产生错误输出值的 bug(GH 2699) -
修复了在
pattern in HDFStore
表达式中,当模式不是有效的正则表达式时的 bug(GH 2694) -
修复了在聚合布尔数据时的性能问题(GH 2692)
-
当给定布尔掩码键和一系列新值时,Series setitem 现在将传入值与原始 Series 对齐(GH 2686)
-
修复了在具有非常大数量的组合值的 MultiIndex 级别上执行计数排序时导致的 MemoryError(GH 2684)
-
修复了当索引为具有固定偏移时区的 DatetimeIndex 时绘图失败的 bug(GH 2683)
-
当偏移量超过 5 个工作日且起始日期为周末时,修正了工作日减法逻辑(GH 2680)
-
修复了 C 文件解析器行为,当文件列数多于数据时(GH 2668)
-
修复了文件读取器错误,当存在隐式列和指定的
usecols
值时,列与数据对齐不正确 -
具有数字或日期时间索引的 DataFrames 现在在绘图之前进行排序(GH 2609)
-
修复了当传递列、索引但记录为空时的 DataFrame.from_records 错误(GH 2633)
-
修复了 Series 操作中 dtype 为 datetime64 时的几个 bug(GH 2689,GH 2629,GH 2626)
查看完整的发布说明或 GitHub 上的问题跟踪器以获取完整列表。
贡献者
总共有 17 人为此版本贡献了补丁。名字后面带有“+”的人第一次贡献了补丁。
-
Andy Hayden +
-
Anton I. Sipos +
-
Chang She
-
Christopher Whelan
-
Damien Garaud +
-
Dan Allan +
-
Dieter Vandenbussche
-
Garrett Drapala +
-
Jay Parlar +
-
Thouis (Ray) Jones +
-
Vincent Arel-Bundock +
-
Wes McKinney
-
elpres
-
herrfz +
-
jreback
-
svaksha +
-
y-p
版本 0.10.0(2012 年 12 月 17 日)
这是从 0.9.1 开始的一个重大版本,包括许多新功能和增强功能,以及大量的错误修复。还有一些重要的 API 更改,长期使用 pandas 的用户应该密切关注。
文件解析新功能
分隔文件解析引擎(read_csv
和read_table
的核心)已经从头开始重写,现在在解析时使用的内存量只有原来的一小部分,而且在大多数情况下快 40%或更快(在某些情况下快得多)。
还有许多新功能:
-
通过
encoding
选项大大改进了 Unicode 处理。 -
列过滤(
usecols
) -
Dtype 规范(
dtype
参数) -
能够指定要识别为 True/False 的字符串
-
能够生成 NumPy 记录数组(
as_recarray
) -
高性能的
delim_whitespace
选项 -
十进制格式(例如欧洲格式)规范
-
更容易的 CSV 方言选项:
escapechar
,lineterminator
,quotechar
等。 -
更健壮地处理野外观察到的许多异常文件类型
API 更改
已弃用的 DataFrame BINOP TimeSeries 特殊情况行为
DataFrame 和 Series 之间的二进制操作的默认行为一直是在 DataFrame 的列上对齐并向下广播行,除非DataFrame 包含时间序列的特殊情况。由于现在为每个二进制运算符都有方法,使您能够指定如何广播,我们正在逐步淘汰这种特殊情况(Python 之禅:特例并不足以打破规则)。这就是我所说的:
In [1]: import pandas as pd
In [2]: df = pd.DataFrame(np.random.randn(6, 4), index=pd.date_range("1/1/2000", periods=6))
In [3]: df
Out[3]:
0 1 2 3
2000-01-01 0.469112 -0.282863 -1.509059 -1.135632
2000-01-02 1.212112 -0.173215 0.119209 -1.044236
2000-01-03 -0.861849 -2.104569 -0.494929 1.071804
2000-01-04 0.721555 -0.706771 -1.039575 0.271860
2000-01-05 -0.424972 0.567020 0.276232 -1.087401
2000-01-06 -0.673690 0.113648 -1.478427 0.524988
# deprecated now
In [4]: df - df[0]
Out[4]:
0 1 ... 2000-01-05 00:00:00 2000-01-06 00:00:00
2000-01-01 NaN NaN ... NaN NaN
2000-01-02 NaN NaN ... NaN NaN
2000-01-03 NaN NaN ... NaN NaN
2000-01-04 NaN NaN ... NaN NaN
2000-01-05 NaN NaN ... NaN NaN
2000-01-06 NaN NaN ... NaN NaN
[6 rows x 10 columns]
# Change your code to
In [5]: df.sub(df[0], axis=0) # align on axis 0 (rows)
Out[5]:
0 1 2 3
2000-01-01 0.0 -0.751976 -1.978171 -1.604745
2000-01-02 0.0 -1.385327 -1.092903 -2.256348
2000-01-03 0.0 -1.242720 0.366920 1.933653
2000-01-04 0.0 -1.428326 -1.761130 -0.449695
2000-01-05 0.0 0.991993 0.701204 -0.662428
2000-01-06 0.0 0.787338 -0.804737 1.198677
在 0.10.x 系列中会收到弃用警告,并且弃用的功能将在 0.11 或更高版本中删除。
更改了重新取样的默认行为
默认时间序列resample
分箱行为的日常D
和更高频率已更改为closed='left',label='left'
。较低频率不受影响。先前的默认值对用户造成了很多困惑,特别是将数据重新取样到每日频率(将聚合组标记为间隔的结束:下一天)。
In [1]: dates = pd.date_range('1/1/2000', '1/5/2000', freq='4h')
In [2]: series = pd.Series(np.arange(len(dates)), index=dates)
In [3]: series
Out[3]:
2000-01-01 00:00:00 0
2000-01-01 04:00:00 1
2000-01-01 08:00:00 2
2000-01-01 12:00:00 3
2000-01-01 16:00:00 4
2000-01-01 20:00:00 5
2000-01-02 00:00:00 6
2000-01-02 04:00:00 7
2000-01-02 08:00:00 8
2000-01-02 12:00:00 9
2000-01-02 16:00:00 10
2000-01-02 20:00:00 11
2000-01-03 00:00:00 12
2000-01-03 04:00:00 13
2000-01-03 08:00:00 14
2000-01-03 12:00:00 15
2000-01-03 16:00:00 16
2000-01-03 20:00:00 17
2000-01-04 00:00:00 18
2000-01-04 04:00:00 19
2000-01-04 08:00:00 20
2000-01-04 12:00:00 21
2000-01-04 16:00:00 22
2000-01-04 20:00:00 23
2000-01-05 00:00:00 24
Freq: 4H, dtype: int64
In [4]: series.resample('D', how='sum')
Out[4]:
2000-01-01 15
2000-01-02 51
2000-01-03 87
2000-01-04 123
2000-01-05 24
Freq: D, dtype: int64
In [5]: # old behavior
In [6]: series.resample('D', how='sum', closed='right', label='right')
Out[6]:
2000-01-01 0
2000-01-02 21
2000-01-03 57
2000-01-04 93
2000-01-05 129
Freq: D, dtype: int64
- 无穷大和负无穷大不再被
isnull
和notnull
视为 NA。它们曾经是早期 pandas 的遗留物。可以通过mode.use_inf_as_null
选项全局重新启用此行为:
In [6]: s = pd.Series([1.5, np.inf, 3.4, -np.inf])
In [7]: pd.isnull(s)
Out[7]:
0 False
1 False
2 False
3 False
Length: 4, dtype: bool
In [8]: s.fillna(0)
Out[8]:
0 1.500000
1 inf
2 3.400000
3 -inf
Length: 4, dtype: float64
In [9]: pd.set_option('use_inf_as_null', True)
In [10]: pd.isnull(s)
Out[10]:
0 False
1 True
2 False
3 True
Length: 4, dtype: bool
In [11]: s.fillna(0)
Out[11]:
0 1.5
1 0.0
2 3.4
3 0.0
Length: 4, dtype: float64
In [12]: pd.reset_option('use_inf_as_null')
-
带有
inplace
选项的方法现在都返回None
而不是调用对象。例如,像df = df.fillna(0, inplace=True)
这样编写的代码可能会停止工作。要修复,只需删除不必要的变量赋值。 -
pandas.merge
不再默认对组键进行排序(sort=False
)。出于性能原因而这样做:组键排序通常是计算中较昂贵的部分之一,而且通常是不必要的。 -
对于没有标题的文件,默认的列名已更改为整数
0
到N - 1
。这是为了与没有指定列的 DataFrame 构造函数创建一致性。可以通过指定prefix='X'
来重现 v0.9.0 行为(名称为X0
,X1
,...):
In [6]: import io
In [7]: data = """
...: a,b,c
...: 1,Yes,2
...: 3,No,4
...: """
...:
In [8]: print(data)
a,b,c
1,Yes,2
3,No,4
In [9]: pd.read_csv(io.StringIO(data), header=None)
Out[9]:
0 1 2
0 a b c
1 1 Yes 2
2 3 No 4
In [10]: pd.read_csv(io.StringIO(data), header=None, prefix="X")
Out[10]:
X0 X1 X2
0 a b c
1 1 Yes 2
2 3 No 4
- 值如
'Yes'
和'No'
默认情况下不被解释为布尔值,尽管可以通过新的true_values
和false_values
参数来控制:
In [4]: print(data)
a,b,c
1,Yes,2
3,No,4
In [5]: pd.read_csv(io.StringIO(data))
Out[5]:
a b c
0 1 Yes 2
1 3 No 4
In [6]: pd.read_csv(io.StringIO(data), true_values=["Yes"], false_values=["No"])
Out[6]:
a b c
0 1 True 2
1 3 False 4
-
如果在
na_values
参数中传递了来自转换器函数的非字符串值,则文件解析器将不会将其识别为 NA。最好使用replace
函数进行后处理。 -
在 Series 或 DataFrame 上调用
fillna
而不带参数的代码不再有效。你必须指定一个填充值或一个插值方法:
In [6]: s = pd.Series([np.nan, 1.0, 2.0, np.nan, 4])
In [7]: s
Out[7]:
0 NaN
1 1.0
2 2.0
3 NaN
4 4.0
dtype: float64
In [8]: s.fillna(0)
Out[8]:
0 0.0
1 1.0
2 2.0
3 0.0
4 4.0
dtype: float64
In [9]: s.fillna(method="pad")
Out[9]:
0 NaN
1 1.0
2 2.0
3 2.0
4 4.0
dtype: float64
添加了便利方法 ffill
和 bfill
:
In [10]: s.ffill()
Out[10]:
0 NaN
1 1.0
2 2.0
3 2.0
4 4.0
dtype: float64
-
Series.apply
现在将在应用函数的返回值上操作,该返回值本身是一个系列,并可能将结果上转换为 DataFrameIn [11]: def f(x): ....: return pd.Series([x, x ** 2], index=["x", "x²"]) ....: In [12]: s = pd.Series(np.random.rand(5)) In [13]: s Out[13]: 0 0.340445 1 0.984729 2 0.919540 3 0.037772 4 0.861549 dtype: float64 In [14]: s.apply(f) Out[14]: x x² 0 0.340445 0.115903 1 0.984729 0.969691 2 0.919540 0.845555 3 0.037772 0.001427 4 0.861549 0.742267
-
新的 API 函数用于处理 pandas 选项(GH 2097):
get_option
/set_option
- 获取/设置选项的值。部分名称被接受。 -reset_option
- 将一个或多个选项重置为其默认值。部分名称被接受。 -describe_option
- 打印一个或多个选项的描述。当没有参数调用时,打印所有注册的选项。
注意:
set_printoptions
/reset_printoptions
现在已被弃用(但仍在使用),打印选项现在位于 “display.XYZ” 下。例如:In [15]: pd.get_option("display.max_rows") Out[15]: 15
-
to_string()
方法现在始终返回 Unicode 字符串(GH 2224)。
新功能
宽 DataFrame 打印
现在,默认情况下,pandas 不再打印摘要信息,而是将字符串表示跨多行分割:
In [16]: wide_frame = pd.DataFrame(np.random.randn(5, 16))
In [17]: wide_frame
Out[17]:
0 1 2 ... 13 14 15
0 -0.548702 1.467327 -1.015962 ... 1.669052 1.037882 -1.705775
1 -0.919854 -0.042379 1.247642 ... 1.956030 0.017587 -0.016692
2 -0.575247 0.254161 -1.143704 ... 1.211526 0.268520 0.024580
3 -1.577585 0.396823 -0.105381 ... 0.593616 0.884345 1.591431
4 0.141809 0.220390 0.435589 ... -0.392670 0.007207 1.928123
[5 rows x 16 columns]
旧的打印摘要信息的行为可以通过 'expand_frame_repr' 打印选项实现:
In [18]: pd.set_option("expand_frame_repr", False)
In [19]: wide_frame
Out[19]:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 -0.548702 1.467327 -1.015962 -0.483075 1.637550 -1.217659 -0.291519 -1.745505 -0.263952 0.991460 -0.919069 0.266046 -0.709661 1.669052 1.037882 -1.705775
1 -0.919854 -0.042379 1.247642 -0.009920 0.290213 0.495767 0.362949 1.548106 -1.131345 -0.089329 0.337863 -0.945867 -0.932132 1.956030 0.017587 -0.016692
2 -0.575247 0.254161 -1.143704 0.215897 1.193555 -0.077118 -0.408530 -0.862495 1.346061 1.511763 1.627081 -0.990582 -0.441652 1.211526 0.268520 0.024580
3 -1.577585 0.396823 -0.105381 -0.532532 1.453749 1.208843 -0.080952 -0.264610 -0.727965 -0.589346 0.339969 -0.693205 -0.339355 0.593616 0.884345 1.591431
4 0.141809 0.220390 0.435589 0.192451 -0.096701 0.803351 1.715071 -0.708758 -1.202872 -1.814470 1.018601 -0.595447 1.395433 -0.392670 0.007207 1.928123
每行的宽度可以通过 'line_width' 更改(默认为 80):
pd.set_option("line_width", 40)
wide_frame
更新了 PyTables 支持
文档 用于 PyTables Table
格式和对 API 的几处增强。以下是你可以期待的内容。
In [41]: store = pd.HDFStore('store.h5')
In [42]: df = pd.DataFrame(np.random.randn(8, 3),
....: index=pd.date_range('1/1/2000', periods=8),
....: columns=['A', 'B', 'C'])
In [43]: df
Out[43]:
A B C
2000-01-01 -2.036047 0.000830 -0.955697
2000-01-02 -0.898872 -0.725411 0.059904
2000-01-03 -0.449644 1.082900 -1.221265
2000-01-04 0.361078 1.330704 0.855932
2000-01-05 -1.216718 1.488887 0.018993
2000-01-06 -0.877046 0.045976 0.437274
2000-01-07 -0.567182 -0.888657 -0.556383
2000-01-08 0.655457 1.117949 -2.782376
[8 rows x 3 columns]
# appending data frames
In [44]: df1 = df[0:4]
In [45]: df2 = df[4:]
In [46]: store.append('df', df1)
In [47]: store.append('df', df2)
In [48]: store
Out[48]:
<class 'pandas.io.pytables.HDFStore'>
File path: store.h5
/df frame_table (typ->appendable,nrows->8,ncols->3,indexers->[index])
# selecting the entire store
In [49]: store.select('df')
Out[49]:
A B C
2000-01-01 -2.036047 0.000830 -0.955697
2000-01-02 -0.898872 -0.725411 0.059904
2000-01-03 -0.449644 1.082900 -1.221265
2000-01-04 0.361078 1.330704 0.855932
2000-01-05 -1.216718 1.488887 0.018993
2000-01-06 -0.877046 0.045976 0.437274
2000-01-07 -0.567182 -0.888657 -0.556383
2000-01-08 0.655457 1.117949 -2.782376
[8 rows x 3 columns]
In [50]: wp = pd.Panel(np.random.randn(2, 5, 4), items=['Item1', 'Item2'],
....: major_axis=pd.date_range('1/1/2000', periods=5),
....: minor_axis=['A', 'B', 'C', 'D'])
In [51]: wp
Out[51]:
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 5 (major_axis) x 4 (minor_axis)
Items axis: Item1 to Item2
Major_axis axis: 2000-01-01 00:00:00 to 2000-01-05 00:00:00
Minor_axis axis: A to D
# storing a panel
In [52]: store.append('wp', wp)
# selecting via A QUERY
In [53]: store.select('wp', [pd.Term('major_axis>20000102'),
....: pd.Term('minor_axis', '=', ['A', 'B'])])
....:
Out[53]:
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 3 (major_axis) x 2 (minor_axis)
Items axis: Item1 to Item2
Major_axis axis: 2000-01-03 00:00:00 to 2000-01-05 00:00:00
Minor_axis axis: A to B
# removing data from tables
In [54]: store.remove('wp', pd.Term('major_axis>20000103'))
Out[54]: 8
In [55]: store.select('wp')
Out[55]:
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 3 (major_axis) x 4 (minor_axis)
Items axis: Item1 to Item2
Major_axis axis: 2000-01-01 00:00:00 to 2000-01-03 00:00:00
Minor_axis axis: A to D
# deleting a store
In [56]: del store['df']
In [57]: store
Out[57]:
<class 'pandas.io.pytables.HDFStore'>
File path: store.h5
/wp wide_table (typ->appendable,nrows->12,ncols->2,indexers->[major_axis,minor_axis])
增强功能
-
添加了分层键的能力
In [58]: store.put('foo/bar/bah', df) In [59]: store.append('food/orange', df) In [60]: store.append('food/apple', df) In [61]: store Out[61]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 /foo/bar/bah frame (shape->[8,3]) /food/apple frame_table (typ->appendable,nrows->8,ncols->3,indexers->[index]) /food/orange frame_table (typ->appendable,nrows->8,ncols->3,indexers->[index]) /wp wide_table (typ->appendable,nrows->12,ncols->2,indexers->[major_axis,minor_axis]) # remove all nodes under this level In [62]: store.remove('food') In [63]: store Out[63]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 /foo/bar/bah frame (shape->[8,3]) /wp wide_table (typ->appendable,nrows->12,ncols->2,indexers->[major_axis,minor_axis])
-
添加了混合类型支持!
In [64]: df['string'] = 'string' In [65]: df['int'] = 1 In [66]: store.append('df', df) In [67]: df1 = store.select('df') In [68]: df1 Out[68]: A B C string int 2000-01-01 -2.036047 0.000830 -0.955697 string 1 2000-01-02 -0.898872 -0.725411 0.059904 string 1 2000-01-03 -0.449644 1.082900 -1.221265 string 1 2000-01-04 0.361078 1.330704 0.855932 string 1 2000-01-05 -1.216718 1.488887 0.018993 string 1 2000-01-06 -0.877046 0.045976 0.437274 string 1 2000-01-07 -0.567182 -0.888657 -0.556383 string 1 2000-01-08 0.655457 1.117949 -2.782376 string 1 [8 rows x 5 columns] In [69]: df1.get_dtype_counts() Out[69]: float64 3 int64 1 object 1 dtype: int64
-
提升了表格写入的性能
-
支持任意索引维度
-
SparseSeries
现在具有density
属性(GH 2384) -
启用
Series.str.strip/lstrip/rstrip
方法以接受一个输入参数来去除任意字符(GH 2411) -
在
melt
中实现value_vars
来限制值到某些列,并将melt
添加到 pandas 命名空间(GH 2412)
错误修复
-
添加了指定条件的
Term
方法(GH 1996)。 -
del store['df']
现在调用store.remove('df')
来删除存储 -
删除连续行比以前快得多
-
可以在表创建中指定
min_itemsize
参数以强制索引列的最小大小(以前的实现将根据第一次附加设置列大小) -
通过
create_table_index
进行索引支持(需要 PyTables >= 2.3)(GH 698)。 -
如果表格未通过
put
先创建,则在存储时会失败 -
修复了加载腌制的数据框后丢失属性的问题(GH2431)
-
对选择和删除进行了次要更改:只有在提供了 where 时才需要表(而不是 None)
兼容性
HDFStore
的 0.10 版本对于读取在 pandas 先前版本中创建的表是向后兼容的,但是使用先前的(未记录的)方法的查询术语不受支持。您必须读取整个文件并使用新格式写出才能利用更新。
N 维面板(实验性)
添加了对 Panel4D 的实验性支持和用于创建 n 维命名面板的工厂函数。以下是你可以期待的一些内容。
In [58]: p4d = Panel4D(np.random.randn(2, 2, 5, 4),
....: labels=['Label1','Label2'],
....: items=['Item1', 'Item2'],
....: major_axis=date_range('1/1/2000', periods=5),
....: minor_axis=['A', 'B', 'C', 'D'])
....:
In [59]: p4d
Out[59]:
<class 'pandas.core.panelnd.Panel4D'>
Dimensions: 2 (labels) x 2 (items) x 5 (major_axis) x 4 (minor_axis)
Labels axis: Label1 to Label2
Items axis: Item1 to Item2
Major_axis axis: 2000-01-01 00:00:00 to 2000-01-05 00:00:00
Minor_axis axis: A to D
有关完整发布说明,请参见完整发布说明或 GitHub 上的问题跟踪器。
贡献者
共有 26 人为此版本提交了补丁。名字旁边有“+”符号的人第一次贡献了补丁。
-
A. Flaxman +
-
Abraham Flaxman
-
Adam Obeng +
-
Brenda Moon +
-
Chang She
-
Chris Mulligan +
-
Dieter Vandenbussche
-
Donald Curtis +
-
Jay Bourque +
-
Jeff Reback +
-
Justin C Johnson +
-
K.-Michael Aye
-
Keith Hughitt +
-
Ken Van Haren +
-
Laurent Gautier +
-
Luke Lee +
-
Martin Blais
-
Tobias Brandt +
-
Wes McKinney
-
Wouter Overmeire
-
alex arsenovic +
-
jreback +
-
locojaydev +
-
timmie
-
y-p
-
zach powers +
文件解析新功能
分隔文件解析引擎(read_csv
和read_table
的核心)已从头开始重写,现在在解析时使用的内存量大大减少,而且在大多数用例中快 40%或更多(在某些情况下快得多)。
还有许多新功能:
-
通过
encoding
选项大大改进了 Unicode 处理。 -
列过滤(
usecols
) -
Dtype 规范(
dtype
参数) -
能够指定要识别为 True/False 的字符串
-
能够产生 NumPy 记录数组(
as_recarray
) -
高性能
delim_whitespace
选项 -
十进制格式(例如欧洲格式)规范
-
更易于使用的 CSV 方言选项:
escapechar
、lineterminator
、quotechar
等。 -
更加健壮地处理了野外观察到的许多异常文件类型
API 更改
已弃用的 DataFrame BINOP TimeSeries 特殊情况行为
DataFrame 和 Series 之间的二元操作的默认行为始终是根据 DataFrame 的列对齐并向下广播行,除了DataFrame 包含时间序列的特殊情况。由于现在每个二元运算符都有一个方法,您可以指定要如何广播,我们正在逐步淘汰这种特殊情况(Python 之禅:特殊情况并不足以打破规则)。我说的是这个:
In [1]: import pandas as pd
In [2]: df = pd.DataFrame(np.random.randn(6, 4), index=pd.date_range("1/1/2000", periods=6))
In [3]: df
Out[3]:
0 1 2 3
2000-01-01 0.469112 -0.282863 -1.509059 -1.135632
2000-01-02 1.212112 -0.173215 0.119209 -1.044236
2000-01-03 -0.861849 -2.104569 -0.494929 1.071804
2000-01-04 0.721555 -0.706771 -1.039575 0.271860
2000-01-05 -0.424972 0.567020 0.276232 -1.087401
2000-01-06 -0.673690 0.113648 -1.478427 0.524988
# deprecated now
In [4]: df - df[0]
Out[4]:
0 1 ... 2000-01-05 00:00:00 2000-01-06 00:00:00
2000-01-01 NaN NaN ... NaN NaN
2000-01-02 NaN NaN ... NaN NaN
2000-01-03 NaN NaN ... NaN NaN
2000-01-04 NaN NaN ... NaN NaN
2000-01-05 NaN NaN ... NaN NaN
2000-01-06 NaN NaN ... NaN NaN
[6 rows x 10 columns]
# Change your code to
In [5]: df.sub(df[0], axis=0) # align on axis 0 (rows)
Out[5]:
0 1 2 3
2000-01-01 0.0 -0.751976 -1.978171 -1.604745
2000-01-02 0.0 -1.385327 -1.092903 -2.256348
2000-01-03 0.0 -1.242720 0.366920 1.933653
2000-01-04 0.0 -1.428326 -1.761130 -0.449695
2000-01-05 0.0 0.991993 0.701204 -0.662428
2000-01-06 0.0 0.787338 -0.804737 1.198677
在 0.10.x 系列中会收到弃用警告,并且弃用的功能将在 0.11 或更高版本中删除。
更改的重新取样默认行为
默认的时间序列resample
分箱行为已更改为closed='left', label='left'
,适用于日频D
和更高频率。较低频率不受影响。先前的默认设置导致用户困惑很大,特别是将数据重新取样到每日频率时(该频率将聚合的组标记为间隔的结束:下一天)。
In [1]: dates = pd.date_range('1/1/2000', '1/5/2000', freq='4h')
In [2]: series = pd.Series(np.arange(len(dates)), index=dates)
In [3]: series
Out[3]:
2000-01-01 00:00:00 0
2000-01-01 04:00:00 1
2000-01-01 08:00:00 2
2000-01-01 12:00:00 3
2000-01-01 16:00:00 4
2000-01-01 20:00:00 5
2000-01-02 00:00:00 6
2000-01-02 04:00:00 7
2000-01-02 08:00:00 8
2000-01-02 12:00:00 9
2000-01-02 16:00:00 10
2000-01-02 20:00:00 11
2000-01-03 00:00:00 12
2000-01-03 04:00:00 13
2000-01-03 08:00:00 14
2000-01-03 12:00:00 15
2000-01-03 16:00:00 16
2000-01-03 20:00:00 17
2000-01-04 00:00:00 18
2000-01-04 04:00:00 19
2000-01-04 08:00:00 20
2000-01-04 12:00:00 21
2000-01-04 16:00:00 22
2000-01-04 20:00:00 23
2000-01-05 00:00:00 24
Freq: 4H, dtype: int64
In [4]: series.resample('D', how='sum')
Out[4]:
2000-01-01 15
2000-01-02 51
2000-01-03 87
2000-01-04 123
2000-01-05 24
Freq: D, dtype: int64
In [5]: # old behavior
In [6]: series.resample('D', how='sum', closed='right', label='right')
Out[6]:
2000-01-01 0
2000-01-02 21
2000-01-03 57
2000-01-04 93
2000-01-05 129
Freq: D, dtype: int64
- 无穷大和负无穷大不再被
isnull
和notnull
视为 NA。它们曾经是早期 pandas 的遗留物。可以通过mode.use_inf_as_null
选项全局重新启用此行为:
In [6]: s = pd.Series([1.5, np.inf, 3.4, -np.inf])
In [7]: pd.isnull(s)
Out[7]:
0 False
1 False
2 False
3 False
Length: 4, dtype: bool
In [8]: s.fillna(0)
Out[8]:
0 1.500000
1 inf
2 3.400000
3 -inf
Length: 4, dtype: float64
In [9]: pd.set_option('use_inf_as_null', True)
In [10]: pd.isnull(s)
Out[10]:
0 False
1 True
2 False
3 True
Length: 4, dtype: bool
In [11]: s.fillna(0)
Out[11]:
0 1.5
1 0.0
2 3.4
3 0.0
Length: 4, dtype: float64
In [12]: pd.reset_option('use_inf_as_null')
-
具有
inplace
选项的方法现在都返回None
,而不是调用对象。例如,像df = df.fillna(0, inplace=True)
这样编写的代码可能会停止工作。要修复,只需删除不必要的变量赋值。 -
pandas.merge
默认不再对组键进行排序(sort=False
)。出于性能原因而这样做:组键排序通常是计算中更昂贵的部分之一,并且通常是不必要的。 -
对于没有标题的文件,默认列名已更改为整数
0
到N - 1
。这是为了与未指定列的 DataFrame 构造函数创建一致性。可以通过指定prefix='X'
来重现 v0.9.0 的行为(名称X0
,X1
,…):
In [6]: import io
In [7]: data = """
...: a,b,c
...: 1,Yes,2
...: 3,No,4
...: """
...:
In [8]: print(data)
a,b,c
1,Yes,2
3,No,4
In [9]: pd.read_csv(io.StringIO(data), header=None)
Out[9]:
0 1 2
0 a b c
1 1 Yes 2
2 3 No 4
In [10]: pd.read_csv(io.StringIO(data), header=None, prefix="X")
Out[10]:
X0 X1 X2
0 a b c
1 1 Yes 2
2 3 No 4
- 像
'Yes'
和'No'
之类的值默认不被解释为布尔值,尽管可以通过新的true_values
和false_values
参数来控制:
In [4]: print(data)
a,b,c
1,Yes,2
3,No,4
In [5]: pd.read_csv(io.StringIO(data))
Out[5]:
a b c
0 1 Yes 2
1 3 No 4
In [6]: pd.read_csv(io.StringIO(data), true_values=["Yes"], false_values=["No"])
Out[6]:
a b c
0 1 True 2
1 3 False 4
-
如果在
na_values
参数中传递由转换器函数产生的非字符串值,则文件解析器将不会将其识别为 NA。最好使用replace
函数进行后处理。 -
在 Series 或 DataFrame 上调用
fillna
而不带参数的代码已不再有效。您必须指定填充值或插值方法:
In [6]: s = pd.Series([np.nan, 1.0, 2.0, np.nan, 4])
In [7]: s
Out[7]:
0 NaN
1 1.0
2 2.0
3 NaN
4 4.0
dtype: float64
In [8]: s.fillna(0)
Out[8]:
0 0.0
1 1.0
2 2.0
3 0.0
4 4.0
dtype: float64
In [9]: s.fillna(method="pad")
Out[9]:
0 NaN
1 1.0
2 2.0
3 2.0
4 4.0
dtype: float64
已添加了方便的方法ffill
和bfill
:
In [10]: s.ffill()
Out[10]:
0 NaN
1 1.0
2 2.0
3 2.0
4 4.0
dtype: float64
-
Series.apply
现在将在应用函数的返回值上操作,该返回值本身是一个系列,并可能将结果向上转换为 DataFrame。In [11]: def f(x): ....: return pd.Series([x, x ** 2], index=["x", "x²"]) ....: In [12]: s = pd.Series(np.random.rand(5)) In [13]: s Out[13]: 0 0.340445 1 0.984729 2 0.919540 3 0.037772 4 0.861549 dtype: float64 In [14]: s.apply(f) Out[14]: x x² 0 0.340445 0.115903 1 0.984729 0.969691 2 0.919540 0.845555 3 0.037772 0.001427 4 0.861549 0.742267
-
用于处理 pandas 选项的新 API 函数(GH 2097):
get_option
/set_option
- 获取/设置选项的值。接受部分名称。 -reset_option
- 将一个或多个选项重置为其默认值。接受部分名称。 -describe_option
- 打印一个或多个选项的描述。当没有参数调用时。打印所有注册的选项。
注意:
set_printoptions
/reset_printoptions
现已弃用(但仍在运行),打印选项现在位于“display.XYZ”下。例如:In [15]: pd.get_option("display.max_rows") Out[15]: 15
-
to_string()
方法现在始终返回 unicode 字符串(GH 2224)。
新特性
宽 DataFrame 打印
pandas 现在默认将字符串表示拆分成多行而不是打印摘要信息:
In [16]: wide_frame = pd.DataFrame(np.random.randn(5, 16))
In [17]: wide_frame
Out[17]:
0 1 2 ... 13 14 15
0 -0.548702 1.467327 -1.015962 ... 1.669052 1.037882 -1.705775
1 -0.919854 -0.042379 1.247642 ... 1.956030 0.017587 -0.016692
2 -0.575247 0.254161 -1.143704 ... 1.211526 0.268520 0.024580
3 -1.577585 0.396823 -0.105381 ... 0.593616 0.884345 1.591431
4 0.141809 0.220390 0.435589 ... -0.392670 0.007207 1.928123
[5 rows x 16 columns]
通过“expand_frame_repr”打印选项可以实现打印摘要信息的旧行为:
In [18]: pd.set_option("expand_frame_repr", False)
In [19]: wide_frame
Out[19]:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 -0.548702 1.467327 -1.015962 -0.483075 1.637550 -1.217659 -0.291519 -1.745505 -0.263952 0.991460 -0.919069 0.266046 -0.709661 1.669052 1.037882 -1.705775
1 -0.919854 -0.042379 1.247642 -0.009920 0.290213 0.495767 0.362949 1.548106 -1.131345 -0.089329 0.337863 -0.945867 -0.932132 1.956030 0.017587 -0.016692
2 -0.575247 0.254161 -1.143704 0.215897 1.193555 -0.077118 -0.408530 -0.862495 1.346061 1.511763 1.627081 -0.990582 -0.441652 1.211526 0.268520 0.024580
3 -1.577585 0.396823 -0.105381 -0.532532 1.453749 1.208843 -0.080952 -0.264610 -0.727965 -0.589346 0.339969 -0.693205 -0.339355 0.593616 0.884345 1.591431
4 0.141809 0.220390 0.435589 0.192451 -0.096701 0.803351 1.715071 -0.708758 -1.202872 -1.814470 1.018601 -0.595447 1.395433 -0.392670 0.007207 1.928123
每行的宽度可以通过‘line_width’进行更改(默认为 80):
pd.set_option("line_width", 40)
wide_frame
更新了 PyTables 支持
PyTables Table
格式的文档和 API 的几个增强。以下是预期的一些内容。
In [41]: store = pd.HDFStore('store.h5')
In [42]: df = pd.DataFrame(np.random.randn(8, 3),
....: index=pd.date_range('1/1/2000', periods=8),
....: columns=['A', 'B', 'C'])
In [43]: df
Out[43]:
A B C
2000-01-01 -2.036047 0.000830 -0.955697
2000-01-02 -0.898872 -0.725411 0.059904
2000-01-03 -0.449644 1.082900 -1.221265
2000-01-04 0.361078 1.330704 0.855932
2000-01-05 -1.216718 1.488887 0.018993
2000-01-06 -0.877046 0.045976 0.437274
2000-01-07 -0.567182 -0.888657 -0.556383
2000-01-08 0.655457 1.117949 -2.782376
[8 rows x 3 columns]
# appending data frames
In [44]: df1 = df[0:4]
In [45]: df2 = df[4:]
In [46]: store.append('df', df1)
In [47]: store.append('df', df2)
In [48]: store
Out[48]:
<class 'pandas.io.pytables.HDFStore'>
File path: store.h5
/df frame_table (typ->appendable,nrows->8,ncols->3,indexers->[index])
# selecting the entire store
In [49]: store.select('df')
Out[49]:
A B C
2000-01-01 -2.036047 0.000830 -0.955697
2000-01-02 -0.898872 -0.725411 0.059904
2000-01-03 -0.449644 1.082900 -1.221265
2000-01-04 0.361078 1.330704 0.855932
2000-01-05 -1.216718 1.488887 0.018993
2000-01-06 -0.877046 0.045976 0.437274
2000-01-07 -0.567182 -0.888657 -0.556383
2000-01-08 0.655457 1.117949 -2.782376
[8 rows x 3 columns]
In [50]: wp = pd.Panel(np.random.randn(2, 5, 4), items=['Item1', 'Item2'],
....: major_axis=pd.date_range('1/1/2000', periods=5),
....: minor_axis=['A', 'B', 'C', 'D'])
In [51]: wp
Out[51]:
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 5 (major_axis) x 4 (minor_axis)
Items axis: Item1 to Item2
Major_axis axis: 2000-01-01 00:00:00 to 2000-01-05 00:00:00
Minor_axis axis: A to D
# storing a panel
In [52]: store.append('wp', wp)
# selecting via A QUERY
In [53]: store.select('wp', [pd.Term('major_axis>20000102'),
....: pd.Term('minor_axis', '=', ['A', 'B'])])
....:
Out[53]:
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 3 (major_axis) x 2 (minor_axis)
Items axis: Item1 to Item2
Major_axis axis: 2000-01-03 00:00:00 to 2000-01-05 00:00:00
Minor_axis axis: A to B
# removing data from tables
In [54]: store.remove('wp', pd.Term('major_axis>20000103'))
Out[54]: 8
In [55]: store.select('wp')
Out[55]:
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 3 (major_axis) x 4 (minor_axis)
Items axis: Item1 to Item2
Major_axis axis: 2000-01-01 00:00:00 to 2000-01-03 00:00:00
Minor_axis axis: A to D
# deleting a store
In [56]: del store['df']
In [57]: store
Out[57]:
<class 'pandas.io.pytables.HDFStore'>
File path: store.h5
/wp wide_table (typ->appendable,nrows->12,ncols->2,indexers->[major_axis,minor_axis])
增强
-
添加了分层键的能力
In [58]: store.put('foo/bar/bah', df) In [59]: store.append('food/orange', df) In [60]: store.append('food/apple', df) In [61]: store Out[61]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 /foo/bar/bah frame (shape->[8,3]) /food/apple frame_table (typ->appendable,nrows->8,ncols->3,indexers->[index]) /food/orange frame_table (typ->appendable,nrows->8,ncols->3,indexers->[index]) /wp wide_table (typ->appendable,nrows->12,ncols->2,indexers->[major_axis,minor_axis]) # remove all nodes under this level In [62]: store.remove('food') In [63]: store Out[63]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 /foo/bar/bah frame (shape->[8,3]) /wp wide_table (typ->appendable,nrows->12,ncols->2,indexers->[major_axis,minor_axis])
-
添加了混合类型支持!
In [64]: df['string'] = 'string' In [65]: df['int'] = 1 In [66]: store.append('df', df) In [67]: df1 = store.select('df') In [68]: df1 Out[68]: A B C string int 2000-01-01 -2.036047 0.000830 -0.955697 string 1 2000-01-02 -0.898872 -0.725411 0.059904 string 1 2000-01-03 -0.449644 1.082900 -1.221265 string 1 2000-01-04 0.361078 1.330704 0.855932 string 1 2000-01-05 -1.216718 1.488887 0.018993 string 1 2000-01-06 -0.877046 0.045976 0.437274 string 1 2000-01-07 -0.567182 -0.888657 -0.556383 string 1 2000-01-08 0.655457 1.117949 -2.782376 string 1 [8 rows x 5 columns] In [69]: df1.get_dtype_counts() Out[69]: float64 3 int64 1 object 1 dtype: int64
-
表写入性能改进
-
对任意索引维度的支持
-
SparseSeries
现在有一个density
属性(GH 2384) -
启用
Series.str.strip/lstrip/rstrip
方法以接受输入参数以剥离任意字符(GH 2411) -
在
melt
中实现value_vars
以限制值到特定列并将melt
添加到 pandas 命名空间(GH 2412)
错误修复
-
添加了指定 where 条件的
Term
方法(GH 1996)。 -
del store['df']
现在调用store.remove('df')
以删除存储 -
删除连续行比以前快得多
-
可以在表创建中指定
min_itemsize
参数以强制索引列的最小大小(先前的实现将根据第一个追加设置列大小) -
通过
create_table_index
实现索引支持(需要 PyTables >= 2.3)(GH 698)。 -
如果表未通过
put
首先创建,则在存储上追加会失败 -
修复了在加载 pickled dataframe 后缺少属性的问题(GH2431)
-
对选择和删除进行了轻微更改:仅在提供了
where
参数时需要一个表(且不为None
)。
兼容性
HDFStore
的 0.10 版本向后兼容,可以读取在 pandas 之前版本中创建的表,但是,使用先前(未记录的)方法的查询条件不受支持。您必须将整个文件读入并使用新格式写出以利用更新。
N 维面板(实验性)
添加了对 Panel4D 的实验性支持和用于创建 n 维命名面板的工厂函数。以下是预期的一些内容。
In [58]: p4d = Panel4D(np.random.randn(2, 2, 5, 4),
....: labels=['Label1','Label2'],
....: items=['Item1', 'Item2'],
....: major_axis=date_range('1/1/2000', periods=5),
....: minor_axis=['A', 'B', 'C', 'D'])
....:
In [59]: p4d
Out[59]:
<class 'pandas.core.panelnd.Panel4D'>
Dimensions: 2 (labels) x 2 (items) x 5 (major_axis) x 4 (minor_axis)
Labels axis: Label1 to Label2
Items axis: Item1 to Item2
Major_axis axis: 2000-01-01 00:00:00 to 2000-01-05 00:00:00
Minor_axis axis: A to D
查看完整发布说明或 GitHub 上的问题跟踪器以获取完整列表。
贡献者
本次发布共有 26 人贡献了补丁。名字后面带有“+”的人是第一次贡献补丁。
-
A. Flaxman +
-
Abraham Flaxman
-
Adam Obeng +
-
Brenda Moon +
-
Chang She
-
Chris Mulligan +
-
Dieter Vandenbussche
-
Donald Curtis +
-
Jay Bourque +
-
Jeff Reback +
-
Justin C Johnson +
-
K.-Michael Aye
-
Keith Hughitt +
-
Ken Van Haren +
-
Laurent Gautier +
-
Luke Lee +
-
Martin Blais
-
Tobias Brandt +
-
Wes McKinney
-
Wouter Overmeire
-
alex arsenovic +
-
jreback +
-
locojaydev +
-
timmie
-
y-p
-
zach powers +
版本 0.9.1(2012 年 11 月 14 日)
这是从 0.9.0 版本的错误修复版本,包括几个新功能和增强功能以及大量的错误修复。新功能包括 DataFrame 和 Series 的按列排序顺序,改进的 rank 方法的 NA 处理,DataFrame 的掩码函数以及 DataFrame 的日内时间序列过滤。
新功能
Series.sort
,DataFrame.sort
和DataFrame.sort_index
现在可以以每列的方式指定以支持多个排序顺序(GH 928)
In [2]: df = pd.DataFrame(np.random.randint(0, 2, (6, 3)), ...: columns=['A', 'B', 'C']) In [3]: df.sort(['A', 'B'], ascending=[1, 0]) Out[3]: A B C 3 0 1 1 4 0 1 1 2 0 0 1 0 1 0 0 1 1 0 0 5 1 0 0
DataFrame.rank
现在支持na_option
参数的附加参数值,因此缺失值可以分配为最大或最小等级(GH 1508,GH 2159)
In [1]: df = pd.DataFrame(np.random.randn(6, 3), columns=['A', 'B', 'C']) In [2]: df.loc[2:4] = np.nan In [3]: df.rank() Out[3]: A B C 0 3.0 2.0 1.0 1 1.0 3.0 2.0 2 NaN NaN NaN 3 NaN NaN NaN 4 NaN NaN NaN 5 2.0 1.0 3.0 In [4]: df.rank(na_option='top') Out[4]: A B C 0 6.0 5.0 4.0 1 4.0 6.0 5.0 2 2.0 2.0 2.0 3 2.0 2.0 2.0 4 2.0 2.0 2.0 5 5.0 4.0 6.0 In [5]: df.rank(na_option='bottom') Out[5]: A B C 0 3.0 2.0 1.0 1 1.0 3.0 2.0 2 5.0 5.0 5.0 3 5.0 5.0 5.0 4 5.0 5.0 5.0 5 2.0 1.0 3.0
DataFrame 目前支持通过与 DataFrame 相同长度的布尔向量进行切片(在
[]
内)。返回的 DataFrame 具有与原始相同数量的列,但在其索引上进行了切片。
In [6]: df = pd.DataFrame(np.random.randn(5, 3), columns=['A', 'B', 'C']) In [7]: df Out[7]: A B C 0 0.276232 -1.087401 -0.673690 1 0.113648 -1.478427 0.524988 2 0.404705 0.577046 -1.715002 3 -1.039268 -0.370647 -1.157892 4 -1.344312 0.844885 1.075770 In [8]: df[df['A'] > 0] Out[8]: A B C 0 0.276232 -1.087401 -0.673690 1 0.113648 -1.478427 0.524988 2 0.404705 0.577046 -1.715002
如果使用基于 DataFrame 的布尔条件(与原始 DataFrame 大小相同)对 DataFrame 进行切片,则将返回与原始 DataFrame 相同大小(索引和列)的 DataFrame,其中不满足布尔条件的元素为
NaN
。这是通过新方法DataFrame.where
实现的。此外,where
接受一个可选的other
参数用于替换。
In [9]: df[df > 0] Out[9]: A B C 0 0.276232 NaN NaN 1 0.113648 NaN 0.524988 2 0.404705 0.577046 NaN 3 NaN NaN NaN 4 NaN 0.844885 1.075770 In [10]: df.where(df > 0) Out[10]: A B C 0 0.276232 NaN NaN 1 0.113648 NaN 0.524988 2 0.404705 0.577046 NaN 3 NaN NaN NaN 4 NaN 0.844885 1.075770 In [11]: df.where(df > 0, -df) Out[11]: A B C 0 0.276232 1.087401 0.673690 1 0.113648 1.478427 0.524988 2 0.404705 0.577046 1.715002 3 1.039268 0.370647 1.157892 4 1.344312 0.844885 1.075770
此外,
where
现在会对齐输入的布尔条件(ndarray 或 DataFrame),从而可以进行设置的部分选择。这类似于通过.ix
进行部分设置(但是在内容而不是轴标签上)
In [12]: df2 = df.copy() In [13]: df2[df2[1:4] > 0] = 3 In [14]: df2 Out[14]: A B C 0 0.276232 -1.087401 -0.673690 1 3.000000 -1.478427 3.000000 2 3.000000 3.000000 -1.715002 3 -1.039268 -0.370647 -1.157892 4 -1.344312 0.844885 1.075770
DataFrame.mask
是where
的逆布尔操作。
In [15]: df.mask(df <= 0) Out[15]: A B C 0 0.276232 NaN NaN 1 0.113648 NaN 0.524988 2 0.404705 0.577046 NaN 3 NaN NaN NaN 4 NaN 0.844885 1.075770
通过它们的列名引用 Excel 列的功能已启用(GH 1936)
In [1]: xl = pd.ExcelFile('data/test.xls') In [2]: xl.parse('Sheet1', index_col=0, parse_dates=True, parse_cols='A:D')
添加了选项来禁用 pandas 风格的刻度定位器和格式化程序,使用
series.plot(x_compat=True)
或pandas.plot_params['x_compat'] = True
(GH 2205)现有的 TimeSeries 方法
at_time
和between_time
已添加到 DataFrame(GH 2149)DataFrame.dot 现在可以接受 ndarrays(GH 2042)
DataFrame.drop 现在支持非唯一索引(GH 2101)
Panel.shift 现在支持负周期(GH 2164)
DataFrame 现在支持一元~运算符(GH 2110)
API 更改
使用 PeriodIndex 对数据进行上采样将导致跨越原始时间窗口的更高频率的 TimeSeries
In [1]: prng = pd.period_range('2012Q1', periods=2, freq='Q') In [2]: s = pd.Series(np.random.randn(len(prng)), prng) In [4]: s.resample('M') Out[4]: 2012-01 -1.471992 2012-02 NaN 2012-03 NaN 2012-04 -0.493593 2012-05 NaN 2012-06 NaN Freq: M, dtype: float64
In [16]: p = pd.Period('2012') In [17]: p.end_time Out[17]: Timestamp('2012-12-31 23:59:59.999999999')
文件解析器不再对具有自定义转换器指定的列强制转换为浮点数或布尔值(GH 2184)
In [18]: import io In [19]: data = ('A,B,C\n' ....: '00001,001,5\n' ....: '00002,002,6') ....: In [20]: pd.read_csv(io.StringIO(data), converters={'A': lambda x: x.strip()}) Out[20]: A B C 0 00001 1 5 1 00002 2 6
查看完整的发布说明或 GitHub 上的问题跟踪器以获取完整列表。
贡献者
总共有 11 人为此版本贡献了补丁。名字后面带有“+”的人第一次贡献了补丁。
-
Brenda Moon +
-
Chang She
-
Jeff Reback +
-
Justin C Johnson +
-
K.-Michael Aye
-
Martin Blais
-
Tobias Brandt +
-
Wes McKinney
-
Wouter Overmeire
-
timmie
-
y-p
新功能
Series.sort
,DataFrame.sort
和DataFrame.sort_index
现在可以以每列的方式指定以支持多个排序顺序(GH 928)
In [2]: df = pd.DataFrame(np.random.randint(0, 2, (6, 3)), ...: columns=['A', 'B', 'C']) In [3]: df.sort(['A', 'B'], ascending=[1, 0]) Out[3]: A B C 3 0 1 1 4 0 1 1 2 0 0 1 0 1 0 0 1 1 0 0 5 1 0 0
DataFrame.rank
现在支持na_option
参数的额外参数值,因此可以将缺失值分配为最大或最小等级(GH 1508, GH 2159)
In [1]: df = pd.DataFrame(np.random.randn(6, 3), columns=['A', 'B', 'C']) In [2]: df.loc[2:4] = np.nan In [3]: df.rank() Out[3]: A B C 0 3.0 2.0 1.0 1 1.0 3.0 2.0 2 NaN NaN NaN 3 NaN NaN NaN 4 NaN NaN NaN 5 2.0 1.0 3.0 In [4]: df.rank(na_option='top') Out[4]: A B C 0 6.0 5.0 4.0 1 4.0 6.0 5.0 2 2.0 2.0 2.0 3 2.0 2.0 2.0 4 2.0 2.0 2.0 5 5.0 4.0 6.0 In [5]: df.rank(na_option='bottom') Out[5]: A B C 0 3.0 2.0 1.0 1 1.0 3.0 2.0 2 5.0 5.0 5.0 3 5.0 5.0 5.0 4 5.0 5.0 5.0 5 2.0 1.0 3.0
DataFrame 目前支持通过与 DataFrame 相同长度的布尔向量进行切片(在
[]
内)。返回的 DataFrame 具有与原始相同数量的列,但是在其索引上进行了切片。
In [6]: df = pd.DataFrame(np.random.randn(5, 3), columns=['A', 'B', 'C']) In [7]: df Out[7]: A B C 0 0.276232 -1.087401 -0.673690 1 0.113648 -1.478427 0.524988 2 0.404705 0.577046 -1.715002 3 -1.039268 -0.370647 -1.157892 4 -1.344312 0.844885 1.075770 In [8]: df[df['A'] > 0] Out[8]: A B C 0 0.276232 -1.087401 -0.673690 1 0.113648 -1.478427 0.524988 2 0.404705 0.577046 -1.715002
如果使用基于 DataFrame 的布尔条件(与原始 DataFrame 大小相同)对 DataFrame 进行切片,则将返回与原始 DataFrame 相同大小(索引和列)的 DataFrame,其中不符合布尔条件的元素为
NaN
。这是通过新方法DataFrame.where
实现的。此外,where
接受一个可选的other
参数用于替换。
In [9]: df[df > 0] Out[9]: A B C 0 0.276232 NaN NaN 1 0.113648 NaN 0.524988 2 0.404705 0.577046 NaN 3 NaN NaN NaN 4 NaN 0.844885 1.075770 In [10]: df.where(df > 0) Out[10]: A B C 0 0.276232 NaN NaN 1 0.113648 NaN 0.524988 2 0.404705 0.577046 NaN 3 NaN NaN NaN 4 NaN 0.844885 1.075770 In [11]: df.where(df > 0, -df) Out[11]: A B C 0 0.276232 1.087401 0.673690 1 0.113648 1.478427 0.524988 2 0.404705 0.577046 1.715002 3 1.039268 0.370647 1.157892 4 1.344312 0.844885 1.075770
此外,
where
现在会对输入的布尔条件(ndarray 或 DataFrame)进行对齐,从而可以通过设置进行部分选择。这类似于通过.ix
进行部分设置(但是针对内容而不是轴标签)
In [12]: df2 = df.copy() In [13]: df2[df2[1:4] > 0] = 3 In [14]: df2 Out[14]: A B C 0 0.276232 -1.087401 -0.673690 1 3.000000 -1.478427 3.000000 2 3.000000 3.000000 -1.715002 3 -1.039268 -0.370647 -1.157892 4 -1.344312 0.844885 1.075770
DataFrame.mask
是where
的逆布尔操作。
In [15]: df.mask(df <= 0) Out[15]: A B C 0 0.276232 NaN NaN 1 0.113648 NaN 0.524988 2 0.404705 0.577046 NaN 3 NaN NaN NaN 4 NaN 0.844885 1.075770
通过它们的列名引用 Excel 列已启用(GH 1936)
In [1]: xl = pd.ExcelFile('data/test.xls') In [2]: xl.parse('Sheet1', index_col=0, parse_dates=True, parse_cols='A:D')
添加了选项,可以通过
series.plot(x_compat=True)
或pandas.plot_params['x_compat'] = True
来禁用类似于 pандas 的刻度定位器和格式化程序(GH 2205)现有的 TimeSeries 方法
at_time
和between_time
已添加到 DataFrame 中(GH 2149)DataFrame.dot 现在可以接受 ndarrays (GH 2042)
DataFrame.drop 现在支持非唯一索引 (GH 2101)
Panel.shift 现在支持负周期 (GH 2164)
DataFrame 现在支持一元 ~ 运算符 (GH 2110)
API 变更
使用 PeriodIndex 对数据进行上采样将得到一个跨越原始时间窗口的更高频率的 TimeSeries
In [1]: prng = pd.period_range('2012Q1', periods=2, freq='Q') In [2]: s = pd.Series(np.random.randn(len(prng)), prng) In [4]: s.resample('M') Out[4]: 2012-01 -1.471992 2012-02 NaN 2012-03 NaN 2012-04 -0.493593 2012-05 NaN 2012-06 NaN Freq: M, dtype: float64
In [16]: p = pd.Period('2012') In [17]: p.end_time Out[17]: Timestamp('2012-12-31 23:59:59.999999999')
文件解析器不再强制将具有自定义转换器的列转换为 float 或 bool (GH 2184)
In [18]: import io In [19]: data = ('A,B,C\n' ....: '00001,001,5\n' ....: '00002,002,6') ....: In [20]: pd.read_csv(io.StringIO(data), converters={'A': lambda x: x.strip()}) Out[20]: A B C 0 00001 1 5 1 00002 2 6
查看完整的发布说明或 GitHub 上的问题跟踪器以获取完整列表。
贡献者
共有 11 人为此版本贡献了补丁。名字后面带有“+”符号的人是第一次贡献补丁的。
-
Brenda Moon +
-
Chang She
-
Jeff Reback +
-
Justin C Johnson +
-
K.-Michael Aye
-
Martin Blais
-
Tobias Brandt +
-
Wes McKinney
-
Wouter Overmeire
-
timmie
-
y-p
版本 0.9.0(2012 年 10 月 7 日)
这是从 0.8.1 版本开始的一个重要更新,包括多项新功能和增强功能,以及大量的错误修复。新功能包括 Series.str
的向量化 Unicode 编码/解码、DataFrame 的 to_latex
方法、更灵活的布尔值解析,以及从 Yahoo! Finance 下载期权数据的功能。
新功能
- 为 Series.str 中的向量化字符串处理方法 添加了
encode
和decode
以处理 Unicode (GH 1706)。- 添加
DataFrame.to_latex
方法 (GH 1735)。- 为所有
rolling_*
操作添加便捷的扩展窗口等价物 (GH 1785)。- 为了从 Yahoo! Finance 获取期权数据,为 pandas.io.data 添加 Options 类 (GH 1748,GH 1739)。
- 更灵活的布尔值解析(是,否,TRUE,FALSE 等) (GH 1691,GH 1295)。
- 在
Series.reset_index
中添加level
参数。TimeSeries.between_time
现在可以跨越午夜选择时间 (GH 1871)。Series
构造函数现在可以接受生成器作为输入 (GH 1679)。DataFrame.dropna
现在可以接受多个轴(元组/列表)作为输入 (GH 924)。- 在
ExcelFile.parse
中启用skip_footer
参数 (GH 1843)。
API 变更。
- 当
header=None
且没有列名传递给read_csv
等函数时,默认列名已更改为更符合 Python 风格且更易于属性访问的名称。
In [1]: import io
In [2]: data = """
...: 0,0,1
...: 1,1,0
...: 0,1,0
...: """
...:
In [3]: df = pd.read_csv(io.StringIO(data), header=None)
In [4]: df
Out[4]:
0 1 2
0 0 0 1
1 1 1 0
2 0 1 0
- 从另一个
Series
创建Series
,传递一个索引,将导致重新索引发生在内部,而不是将Series
视为 ndarray。从技术上讲,像Series(df[col1], index=df[col2])
这样的不正确用法之前是“偶然发生”的(这从未是打算的),在某些情况下会导致所有 NASeries
。为了更清晰明了:
In [5]: s1 = pd.Series([1, 2, 3])
In [6]: s1
Out[6]:
0 1
1 2
2 3
dtype: int64
In [7]: s2 = pd.Series(s1, index=["foo", "bar", "baz"])
In [8]: s2
Out[8]:
foo NaN
bar NaN
baz NaN
dtype: float64
-
从 PeriodIndex 中移除了已弃用的
day_of_year
API,请使用dayofyear
(GH 1723)。 -
不要在导入时修改 NumPy 的 suppress printoption 为 True。
-
DataFrame 的内部 HDF5 数据布局已被转置。遗留文件仍然可以被 HDFStore 读取 (GH 1834,GH 1824)。
-
移除了遗留的冗余代码:pandas.stats.misc.quantileTS。
-
为 Period 表示添加 ISO8601 格式:monthly、daily 和向下 (GH 1776)。
-
空 DataFrame 列现在以对象 dtype 创建。这将防止在数据存在与否的代码中发生的一类 TypeErrors(例如,SQL 查询有结果时的 dtype 取决于数据的存在与否)(GH 1783)
-
使用 ix 设置 DataFrame/Panel 的部分现在会对齐输入的 Series/DataFrame (GH 1630)
-
GroupBy
中的first
和last
方法不再丢弃非数字列 (GH 1809) -
解决了文本解析器中指定自定义 NA 值的不一致性。类型为 dict 的
na_values
现在不会覆盖默认的 NA,除非显式将keep_default_na
设置为 false(GH 1657) -
DataFrame.dot
现在不会进行数据对齐,并且可以与 Series 一起使用 (GH 1915)
查看 完整发布说明 或 GitHub 上的问题跟踪器以获取完整列表。
贡献者
共有 24 人为此版本贡献了补丁。带有 “+” 的人名表示首次贡献补丁。
-
Chang She
-
Christopher Whelan +
-
Dan Miller +
-
Daniel Shapiro +
-
Dieter Vandenbussche
-
Doug Coleman +
-
John-Colvin +
-
Johnny +
-
Joshua Leahy +
-
Lars Buitinck +
-
Mark O’Leary +
-
Martin Blais
-
MinRK +
-
Paul Ivanov +
-
Skipper Seabold
-
Spencer Lyon +
-
Taavi Burns +
-
Wes McKinney
-
Wouter Overmeire
-
Yaroslav Halchenko
-
lenolib +
-
tshauck +
-
y-p +
-
Øystein S. Haaland +
新功能
- 在 Series.str 的 向量化字符串处理方法 中添加
encode
和decode
用于 Unicode 处理(GH 1706)- 添加
DataFrame.to_latex
方法 (GH 1735)- 添加所有 rolling_* 操作的便利的扩展窗口等价物(GH 1785)
- 向 pandas.io.data 添加 Options 类,用于从 Yahoo! Finance 获取期权数据(GH 1748、GH 1739)
- 更灵活地解析布尔值(Yes、No、TRUE、FALSE 等)(GH 1691、GH 1295)
- 向
Series.reset_index
添加level
参数TimeSeries.between_time
现在可以在跨越午夜的时间中选择(GH 1871)- Series 构造函数现在可以处理生成器作为输入(GH 1679)
DataFrame.dropna
现在可以接受多个轴(元组/列表)作为输入(GH 924)- 在
ExcelFile.parse
中启用skip_footer
参数 (GH 1843)
API 变更
- 当
header=None
并且没有传递列名到read_csv
等函数时,默认列名已更改为更具 Python 风格且易于属性访问:
In [1]: import io
In [2]: data = """
...: 0,0,1
...: 1,1,0
...: 0,1,0
...: """
...:
In [3]: df = pd.read_csv(io.StringIO(data), header=None)
In [4]: df
Out[4]:
0 1 2
0 0 0 1
1 1 1 0
2 0 1 0
- 从另一个 Series 创建 Series,传递一个索引,将导致内部重新索引而不是像处理 ndarray 一样。技术上不正确的用法,例如
Series(df[col1], index=df[col2])
之前“偶然成功”(这从未打算)将在某些情况下导致所有 NA Series。要非常清楚:
In [5]: s1 = pd.Series([1, 2, 3])
In [6]: s1
Out[6]:
0 1
1 2
2 3
dtype: int64
In [7]: s2 = pd.Series(s1, index=["foo", "bar", "baz"])
In [8]: s2
Out[8]:
foo NaN
bar NaN
baz NaN
dtype: float64
-
从 PeriodIndex 中删除了已弃用的
day_of_year
API,使用dayofyear
(GH 1723) -
不要在导入时修改 NumPy 的 suppress printoption 为 True
-
DataFrame 的内部 HDF5 数据排列已经被转置。旧文件仍然可以被 HDFStore 读取 (GH 1834, GH 1824)
-
删除了遗留的 pandas.stats.misc.quantileTS
-
使用 ISO8601 格式来表示 Period:monthly、daily 等 (GH 1776)
-
空 DataFrame 列现在创建为对象 dtype。这将防止在列的 dtype 取决于数据存在与否时发生的一类 TypeError(例如,具有结果的 SQL 查询) (GH 1783)
-
使用 ix 设置 DataFrame/Panel 的部分现在会对齐输入的 Series/DataFrame (GH 1630)
-
GroupBy
中的first
和last
方法不再删除非数字列 (GH 1809) -
解决了在文本解析器中指定自定义 NA 值时的不一致性。类型为 dict 的 na_values 不再覆盖默认 NA,除非显式设置
keep_default_na
为 false (GH 1657) -
DataFrame.dot
将不再进行数据对齐,并且也适用于 Series (GH 1915)
查看完整的发行说明或 GitHub 上的问题跟踪器以获得完整列表。
贡献者
总共有 24 人为此版本提供了补丁。名字后面带有“+”的人第一次贡献了补丁。
-
Chang She
-
Christopher Whelan +
-
Dan Miller +
-
Daniel Shapiro +
-
Dieter Vandenbussche
-
Doug Coleman +
-
John-Colvin +
-
Johnny +
-
Joshua Leahy +
-
Lars Buitinck +
-
Mark O’Leary +
-
Martin Blais
-
MinRK +
-
Paul Ivanov +
-
Skipper Seabold
-
Spencer Lyon +
-
Taavi Burns +
-
Wes McKinney
-
Wouter Overmeire
-
Yaroslav Halchenko
-
lenolib +
-
tshauck +
-
y-p +
-
Øystein S. Haaland +
版本 0.8.1(2012 年 7 月 22 日)
此版本包含了一些新功能、性能增强和来自 0.8.0 的 30 多个错误修复。新功能包括了显著的 NA 友好字符串处理功能和一系列新的绘图类型和选项。
新功能
性能改进
- 改进了滚动最小值和最大值的实现(感谢 Bottleneck!)
- 添加了加速的
'median'
GroupBy 选项 (GH 1358)- 显著提高了使用
DatetimeIndex
或to_datetime
解析 ISO8601 格式日期字符串的性能 (GH 1571)- 改进了单键聚合上的 GroupBy 性能,并与 Categorical 类型一起使用
- 重要的日期时间解析性能改进
贡献者
总共有 5 人为此版本提供了补丁。名字后面带有“+”的人第一次为此提供了补丁。
-
Chang She
-
Skipper Seabold
-
Todd DeLuca +
-
Vytautas Jancauskas
-
Wes McKinney
新功能
性能改进
- 改进了滚动最小值和最大值的实现(感谢Bottleneck!)
- 添加加速的
'median'
GroupBy 选项(GH 1358)- 显著改善了使用
DatetimeIndex
或to_datetime
解析 ISO8601 格式日期字符串的性能(GH 1571)- 改进了单键聚合的 GroupBy 性能,并与分类类型一起使用
- 重要的日期时间解析性能改进
贡献者
总共有 5 人为这个版本贡献了补丁。名字旁边带有“+”的人第一次贡献了补丁。
-
Chang She
-
Skipper Seabold
-
Todd DeLuca +
-
Vytautas Jancauskas
-
Wes McKinney
版本 0.8.0(2012 年 6 月 29 日)
这是从 0.7.3 到 0.8.0 的一次重大更新,包括对时间序列处理和处理基础设施的大量工作,以及库中许多新功能。它包含了来自 20 多位不同作者的 700 多次提交。大多数 pandas 0.7.3 及更早版本的用户升级时不应遇到任何问题,但由于迁移到 NumPy datetime64 dtype,可能存在一些潜在的错误和不兼容性。如果需要,0.8.1 版本将尽快修复仍然存在的不兼容性。请参阅 GitHub 上的完整发布说明或问题跟踪器,获取完整列表。
支持非唯一索引
所有对象现在都可以与非唯一索引一起工作。数据对齐/连接操作按照 SQL 连接语义进行工作(包括,如果应用,多对多连接中的索引重复)
NumPy datetime64 dtype 和 1.6 依赖
现在使用 NumPy 的 datetime64 dtype 表示时间序列数据;因此,pandas 0.8.0 现在至少需要 NumPy 1.6。它已经经过测试,并且已验证可与 NumPy 的开发版本(1.7+)一起使用,其中包括一些重要的面向用户的 API 更改。NumPy 1.6 还存在一些与纳秒分辨率数据有关的错误,因此我建议您避免使用 NumPy 1.6 的 datetime64 API 函数(尽管它们受限),并且只使用 pandas 提供的接口与此数据交互。
请参阅 0.8.0 版本结尾处的“迁移”指南,列出了将遗留代码库从 pandas 0.7 或更早版本迁移到 0.8.0 版本时可能遇到的问题。
将为使用旧版 NumPy < 1.6 的遗留 0.7.x 系列提供错误修复,如出现。0.7.x 系列将不会进行更多的开发,仅提供错误修复。
时间序列的更改和改进
注意
通过此版本,传统的 scikits.timeseries 用户应该能够将其代码移植为使用 pandas。
注意
请参阅文档以获取 pandas 时间序列 API 的概述。
-
新的 datetime64 表示加速了连接操作和数据对齐,减少了内存使用,并且大大提高了序列化/反序列化性能,比 datetime.datetime 要好得多。
-
高性能和灵活的重新采样方法,用于从高频到低频和从低频到高频的转换。支持插值,用户定义的聚合函数,并且可以控制间隔和结果标签的定义方式。还实现了一套高性能的基于 Cython/C 的重新采样函数(包括开-高-低-收)。
-
频率别名的更新,支持频率快捷方式如‘15min’或‘1h30min’。
-
新的 DatetimeIndex 类支持固定频率和不规则时间序列。取代了现在已弃用的 DateRange 类
-
新的
PeriodIndex
和Period
类用于表示时间跨度和执行日历逻辑,包括12 个财政季度频率<timeseries.quarterly>
。这是对 scikits.timeseries 代码库的部分移植和实质性增强。支持在 PeriodIndex 和 DatetimeIndex 之间进行转换 -
新的 Timestamp 数据类型子类
datetime.datetime
,提供相同的接口,同时支持与纳秒分辨率数据的工作。还提供简单的时区转换。 -
增强对时区的支持。为 TimeSeries 和 DataFrame 添加了
tz_convert
和tz_localize
方法。所有时间戳都以 UTC 存储;具有设置时区的 DatetimeIndex 对象的时间戳将被本地化为本地时间。因此,时区转换基本上是免费的。用户现在只需要了解很少关于 pytz 库;只需要时区名称作为字符串。时区感知时间戳仅在它们的 UTC 时间戳匹配时才相等。具有不同时区的时区感知时间序列之间的操作将导致以 UTC 为索引的时间序列。 -
时间序列字符串索引便利/快捷方式:按年份、年份和月份切片,并使用字符串索引值
-
增强的时间序列绘图;适应 scikits.timeseries 基于 matplotlib 的绘图代码
-
新的
date_range
、bdate_range
和period_range
工厂函数 -
强大的频率推断函数
infer_freq
和inferred_freq
属性,可在 DatetimeIndex 构造时推断频率 -
to_datetime 函数高效地将字符串数组解析为 DatetimeIndex。DatetimeIndex 将解析字符串数组或列表为 datetime64
-
在 Series 和 DataFrame 列中对 datetime64-dtype 数据的优化支持
-
新的 NaT(Not-a-Time)类型用于表示时间戳数组中的NA
-
优化 Series.asof 以查找时间戳数组的“截至”值
-
毫秒、微秒、纳秒日期偏移对象
-
可以使用 datetime.time 对象索引时间序列,以选择特定一天中的时间的所有数据(
TimeSeries.at_time
)或两个时间之间的数据(TimeSeries.between_time
) -
添加 tshift 方法,用于使用索引的频率(如果有)进行领先/滞后,而不是使用 shift 进行简单的领先/滞后
其他新功能
-
新的 cut 和
qcut
函数(类似于 R 的 cut 函数),用于通过将值分成基于值的(cut
)或基于分位数的(qcut
)箱来从连续变量计算分类变量 -
将
Factor
重命名为Categorical
并添加一些可用性功能 -
为 fillna/reindex 添加 limit 参数
-
在 GroupBy 中更灵活地应用多个函数,并且可以传递列表(名称、函数)元组以按特定顺序获取结果并给定名称
-
为高效替换添加灵活的 replace 方法
-
改进了 read_csv/read_table 用于读取时间序列数据和将多列转换为日期的功能
-
为解析器函数(read_csv 等)添加 comments 选项。
-
为解析器函数添加 dayfirst 选项,以解析国际 DD/MM/YYYY 日期
-
允许用户指定 CSV 读取器的 方言 以控制引号等。
-
处理 千位分隔符 在 read_csv 中以改善整数解析。
-
启用一次性取消多个级别的 unstacking。缓解
pivot_table
中的错误(引入空列) -
移至基于 klib 的哈希表进行索引;比 Python 的 dict 性能更好,内存使用更少
-
添加了优化的 GroupBy 函数 first、last、min、max 和 prod
-
新的 ordered_merge 函数
-
为 DataFrame、Series 添加灵活的 比较 实例方法 eq、ne、lt、gt 等。
-
改进 scatter_matrix 绘图函数,并在对角线上添加直方图或核密度估计
-
为密度图添加 ‘kde’ 绘图选项
-
支持通过 rpy2 将 DataFrame 转换为 R data.frame
-
改进了 Series 和 DataFrame 中复数的支持
-
对所有数据结构添加
pct_change
方法 -
为 DataFrame 控制台输出添加 max_colwidth 配置选项
-
使用索引值对 Series 值进行 插值
-
可以从 GroupBy 中选择多列
-
为 Series/DataFrame 添加 update 方法以原地更新值
-
为 DataFrame 添加
any
和all
方法
新的绘图方法
import pandas as pd
fx = pd.read_pickle("data/fx_prices")
import matplotlib.pyplot as plt
Series.plot
现在支持 secondary_y
选项:
plt.figure()
fx["FR"].plot(style="g")
fx["IT"].plot(style="k--", secondary_y=True)
2012 年 GSOC 参与者 Vytautas Jancauskas 添加了许多新的绘图类型。例如,'kde'
是一个新选项:
s = pd.Series(
np.concatenate((np.random.randn(1000), np.random.randn(1000) * 0.5 + 3))
)
plt.figure()
s.hist(density=True, alpha=0.2)
s.plot(kind="kde")
查看 绘图页面 了解更多信息。
其他 API 更改
- 在时间序列函数中废弃
offset
、time_rule
和timeRule
参数名称。将在 pandas 0.9 或 1.0 之前打印警告。
对于 pandas <= 0.7.3 用户可能出现的移植问题
在 pandas 0.8.0 中可能会影响你的主要变化是时间序列索引使用了 NumPy 的 datetime64
数据类型,而不是 Python 内置的 datetime.datetime
对象的 dtype=object
数组。DateRange
已被 DatetimeIndex
取代,但行为上基本相同。但是,如果你的代码将以前包含 datetime.datetime
值的 DateRange
或 Index
对象转换为普通的 NumPy 数组,那么使用标量值的代���可能存在潜在的错误,因为你正在将控制权交给 NumPy:
In [1]: import datetime
In [2]: rng = pd.date_range("1/1/2000", periods=10)
In [3]: rng[5]
Out[3]: Timestamp('2000-01-06 00:00:00')
In [4]: isinstance(rng[5], datetime.datetime)
Out[4]: True
In [5]: rng_asarray = np.asarray(rng)
In [6]: scalar_val = rng_asarray[5]
In [7]: type(scalar_val)
Out[7]: numpy.datetime64
pandas 的 Timestamp
对象是 datetime.datetime
的子类,支持纳秒(nanosecond
字段存储 0 到 999 之间的纳秒值)。它应该直接替换任何之前使用 datetime.datetime
值的代码。因此,我建议不要将 DatetimeIndex
转换为常规的 NumPy 数组。
如果你的代码需要一个 datetime.datetime
对象数组,你有几个选项。首先,DatetimeIndex
的 astype(object)
方法会产生一个 Timestamp
对象数组:
In [8]: stamp_array = rng.astype(object)
In [9]: stamp_array
Out[9]:
Index([2000-01-01 00:00:00, 2000-01-02 00:00:00, 2000-01-03 00:00:00,
2000-01-04 00:00:00, 2000-01-05 00:00:00, 2000-01-06 00:00:00,
2000-01-07 00:00:00, 2000-01-08 00:00:00, 2000-01-09 00:00:00,
2000-01-10 00:00:00],
dtype='object')
In [10]: stamp_array[5]
Out[10]: Timestamp('2000-01-06 00:00:00')
要获得正确的 datetime.datetime
对象数组,请使用 to_pydatetime
方法:
In [11]: dt_array = rng.to_pydatetime()
In [12]: dt_array
Out[12]:
array([datetime.datetime(2000, 1, 1, 0, 0),
datetime.datetime(2000, 1, 2, 0, 0),
datetime.datetime(2000, 1, 3, 0, 0),
datetime.datetime(2000, 1, 4, 0, 0),
datetime.datetime(2000, 1, 5, 0, 0),
datetime.datetime(2000, 1, 6, 0, 0),
datetime.datetime(2000, 1, 7, 0, 0),
datetime.datetime(2000, 1, 8, 0, 0),
datetime.datetime(2000, 1, 9, 0, 0),
datetime.datetime(2000, 1, 10, 0, 0)], dtype=object)
In [13]: dt_array[5]
Out[13]: datetime.datetime(2000, 1, 6, 0, 0)
matplotlib 知道如何处理 datetime.datetime
,但不知道如何处理 Timestamp 对象。虽然我建议你使用 TimeSeries.plot
绘制时间序列,但你可以使用 to_pydatetime
或为 Timestamp 类型注册一个转换器。有关更多信息,请查看 matplotlib 文档。
警告
在 NumPy 1.6 中,用户界面 API 中存在 nanosecond datetime64 单元的错误。特别是,数组的字符串版本显示垃圾值,并且转换为 dtype=object
也同样存在问题。
In [14]: rng = pd.date_range("1/1/2000", periods=10)
In [15]: rng
Out[15]:
DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03', '2000-01-04',
'2000-01-05', '2000-01-06', '2000-01-07', '2000-01-08',
'2000-01-09', '2000-01-10'],
dtype='datetime64[ns]', freq='D')
In [16]: np.asarray(rng)
Out[16]:
array(['2000-01-01T00:00:00.000000000', '2000-01-02T00:00:00.000000000',
'2000-01-03T00:00:00.000000000', '2000-01-04T00:00:00.000000000',
'2000-01-05T00:00:00.000000000', '2000-01-06T00:00:00.000000000',
'2000-01-07T00:00:00.000000000', '2000-01-08T00:00:00.000000000',
'2000-01-09T00:00:00.000000000', '2000-01-10T00:00:00.000000000'],
dtype='datetime64[ns]')
In [17]: converted = np.asarray(rng, dtype=object)
In [18]: converted[5]
Out[18]: Timestamp('2000-01-06 00:00:00')
相信我:不要惊慌。如果你使用 NumPy 1.6 并且将与 datetime64
值的交互限制在 pandas 的 API 中,那么一切都会很好。数据类型没有问题(内部是一个 64 位整数);所有重要的数据处理都在 pandas 中进行,并经过了严格测试。我强烈建议你不要直接在 NumPy 1.6 中使用 datetime64 数组,只使用 pandas API。
支持非唯一索引:在后一种情况下,你可能有代码位于 try:... catch:
块中,由于索引不唯一而失败。在许多情况下,它将不再失败(一些方法如 append
仍然检查唯一性,除非禁用)。但是,一切并非绝望:你可以检查 index.is_unique
,如果为 False
则显式引发异常,或者转到不同的代码分支。
贡献者
总共有 27 人为这个版本贡献了补丁。名字旁边带有“+”符号的人是第一次贡献补丁的。
-
Adam Klein
-
Chang She
-
David Zaslavsky +
-
Eric Chlebek +
-
Jacques Kvam
-
Kamil Kisiel
-
Kelsey Jordahl +
-
Kieran O’Mahony +
-
Lorenzo Bolla +
-
Luca Beltrame
-
Marc Abramowitz +
-
Mark Wiebe +
-
Paddy Mullen +
-
Peng Yu +
-
Roy Hyunjin Han +
-
RuiDC +
-
Senthil Palanisami +
-
Skipper Seabold
-
Stefan van der Walt +
-
Takafumi Arakaki +
-
Thomas Kluyver
-
Vytautas Jancauskas +
-
Wes McKinney
-
Wouter Overmeire
-
Yaroslav Halchenko
-
thuske +
-
timmie +
支持非唯一索引
所有对象现在都可以使用非唯一索引。数据对齐/连接操作按照 SQL 连接语义进行(包括,在适用的情况下,在多对多连接中复制索引)
NumPy datetime64 dtype 和 1.6 依赖性
时间序列数据现在使用 NumPy 的 datetime64 dtype 表示;因此,pandas 0.8.0 现在至少需要 NumPy 1.6. 它已经被测试并验证可以与 NumPy 的开发版本(1.7+)一起工作,该版本包括一些重要的用户界面 API 更改。NumPy 1.6 也有一些关于纳秒分辨率数据的错误,所以我建议你避免使用 NumPy 1.6 的 datetime64 API 函数(尽管它们是有限的),只使用 pandas 提供的接口与此数据交互。
请参阅 0.8.0 版本部分末尾的“移植”指南,其中列出了用户从 pandas 0.7 或更早版本迁移传统代码库到 0.8.0 可能遇到的问题。
对于传统的 NumPy < 1.6 用户的 0.7.x 系列的错误修复将随着出现而提供。除了错误修复之外,不会再对 0.7.x 进行更多的开发。
时间序列变化和改进
注意
通过此版本发布,传统的 scikits.timeseries 用户应该能够将他们的代码转换为使用 pandas。
注意
请参阅文档以获取 pandas 时间序列 API 概述。
-
新的 datetime64 表示加速连接操作和数据对齐,减少内存使用,并显著提高序列化/反序列化性能,优于 datetime.datetime
-
高性能和灵活的重新采样方法,用于从高到低和低到高频率的转换。支持插值,用户定义的聚合函数,并控制定义间隔和结果标签的方式。还实现了一套高性能的 Cython/C-based 重新采样函数(包括 Open-High-Low-Close)。
-
对频率别名进行了整顿,并支持频率快捷方式,如 ‘15min’ 或 ‘1h30min’
-
新的 DatetimeIndex 类支持固定频率和不规则时间序列。取代了现在已弃用的 DateRange 类
-
新的
PeriodIndex
和Period
类用于表示时间跨度并执行日历逻辑,包括12 财政季度频率 <timeseries.quarterly>
。这是对 scikits.timeseries 代码库的部分移植,也是对其进行了实质性增强。支持在 PeriodIndex 和 DatetimeIndex 之间进行转换 -
新的 Timestamp 数据类型子类
datetime.datetime
,提供相同的接口同时能够处理纳秒分辨率数据。还提供了简单的时区转换。 -
增强了对时区的支持。为 TimeSeries 和 DataFrame 添加了
tz_convert
和tz_localize
方法。所有时间戳都以 UTC 存储;具有设置时区的 DatetimeIndex 对象的时间戳将被本地化为本地时间。因此,时区转换基本上是免费的。现在用户几乎不需要了解 pytz 库;只需要时间区域名称作为字符串即可。时区感知时间戳仅当它们的 UTC 时间戳匹配时才相等。具有不同时区的时区感知时间序列之间的操作将导致 UTC 索引的时间序列。 -
时间序列字符串索引便利功能/快捷方式:切片年份、年份和月份,并使用字符串索引值。
-
加强了时间序列的绘图;改编自 scikits.timeseries 基于 matplotlib 的绘图代码。
-
新增了
date_range
、bdate_range
和period_range
工厂函数。 -
强大的频率推断函数
infer_freq
和 DatetimeIndex 的inferred_freq
属性,可以在构建 DatetimeIndex 时推断频率。 -
to_datetime
函数高效地解析字符串数组为 DatetimeIndex。DatetimeIndex 将数组或字符串列表解析为 datetime64。 -
优化了对 Series 和 DataFrame 列中的 datetime64-dtype 数据的支持。
-
新增了 NaT(Not-a-Time)类型,用于在时间戳数组中表示NA。
-
优化了 Series.asof 以查找数组时间戳的“截止”值。
-
毫秒、微秒、纳秒日期偏移对象
-
可以使用 datetime.time 对象索引时间序列以选择特定一天中的时间(
TimeSeries.at_time
)或两个时间之间的数据(TimeSeries.between_time
)。 -
添加了使用索引的频率(如果有)进行领先/滞后的 tshift 方法,而不是使用 shift 进行朴素的领先/滞后。
其他新功能
-
新增了 cut 和
qcut
函数(类似于 R 的 cut 函数),用于通过将值分箱到基于值的 (cut
) 或基于分位数的 (qcut
) 箱中计算连续变量的分类变量。 -
将
Factor
重命名为Categorical
并添加了一些可用性功能 -
为 fillna/reindex 添加了 limit 参数。
-
在 GroupBy 中进行更灵活的多函数应用,并且可以传递列表(名称,函数)元组以按特定顺序和给定名称获取结果
-
添加了灵活的替换方法,用于高效地替换值
-
加强了 read_csv/read_table 以读取时间序列数据并将多列转换为日期的功能。
-
为解析函数添加了 comments 选项:read_csv 等。
-
为解析国际 DD/MM/YYYY 日期的解析函数添加了 dayfirst 选项。
-
允许用户指定 CSV 阅读器的方言以控制引号等。
-
处理 read_csv 中的千位分隔符以改善整数解析。
-
一次性解除多个级别的 unstacking。减轻
pivot_table
的错误(引入空列) -
使用基于 klib 的哈希表进行索引;性能更好,内存使用更少,比 Python 的 dict 更好
-
添加了优化的 GroupBy 函数:first、last、min、max 和 prod
-
新的 ordered_merge 函数
-
为 DataFrame、Series 添加灵活的比较实例方法 eq、ne、lt、gt 等。
-
改进 scatter_matrix 绘图函数,并在对角线上添加直方图或核密度估计
-
为密度图添加‘kde’绘图选项
-
通过 rpy2 支持将 DataFrame 转换为 R data.frame
-
在 Series 和 DataFrame 中改进对复数的支持
-
为所有数据结构添加
pct_change
方法 -
为 DataFrame 控制台输出添加 max_colwidth 配置选项
-
使用索引值对 Series 值进行插值
-
可以从 GroupBy 中选择多列
-
为 Series/DataFrame 添加 update 方法以就地更新值
-
为 DataFrame 添加
any
和all
方法
新的绘图方法
import pandas as pd
fx = pd.read_pickle("data/fx_prices")
import matplotlib.pyplot as plt
Series.plot
现在支持secondary_y
选项:
plt.figure()
fx["FR"].plot(style="g")
fx["IT"].plot(style="k--", secondary_y=True)
2012 年 GSOC 参与者 Vytautas Jancauskas 添加了许多新的绘图类型。例如,'kde'
是一个新选项:
s = pd.Series(
np.concatenate((np.random.randn(1000), np.random.randn(1000) * 0.5 + 3))
)
plt.figure()
s.hist(density=True, alpha=0.2)
s.plot(kind="kde")
查看绘图页面获取更多信息。
其他 API 更改
- 在时间序列函数中弃用
offset
、time_rule
和timeRule
参数名称。警告将在 pandas 0.9 或 1.0 之前打印。
对于 pandas <= 0.7.3 用户可能存在的迁移问题
在 pandas 0.8.0 中可能影响您的主要更改是时间序列索引使用 NumPy 的datetime64
数据类型,而不是 Python 内置的datetime.datetime
对象的dtype=object
数组。DateRange
已被DatetimeIndex
取代,但行为相同。但是,如果您的代码将以前包含datetime.datetime
值的DateRange
或Index
对象转换为普通的 NumPy 数组,那么您可能会在使用标量值的代码中存在潜在的错误,因为您正在将控制权交给 NumPy:
In [1]: import datetime
In [2]: rng = pd.date_range("1/1/2000", periods=10)
In [3]: rng[5]
Out[3]: Timestamp('2000-01-06 00:00:00')
In [4]: isinstance(rng[5], datetime.datetime)
Out[4]: True
In [5]: rng_asarray = np.asarray(rng)
In [6]: scalar_val = rng_asarray[5]
In [7]: type(scalar_val)
Out[7]: numpy.datetime64
pandas 的Timestamp
对象是datetime.datetime
的子类,支持纳秒(nanosecond
字段存储 0 到 999 之间的纳秒值)。它应直接替换任何以前使用datetime.datetime
值的代码。因此,我建议不将DatetimeIndex
转换为常规的 NumPy 数组。
如果你的代码需要一个 datetime.datetime
对象数组,你有几个选择。首先,DatetimeIndex
的 astype(object)
方法会产生一个 Timestamp
对象数组:
In [8]: stamp_array = rng.astype(object)
In [9]: stamp_array
Out[9]:
Index([2000-01-01 00:00:00, 2000-01-02 00:00:00, 2000-01-03 00:00:00,
2000-01-04 00:00:00, 2000-01-05 00:00:00, 2000-01-06 00:00:00,
2000-01-07 00:00:00, 2000-01-08 00:00:00, 2000-01-09 00:00:00,
2000-01-10 00:00:00],
dtype='object')
In [10]: stamp_array[5]
Out[10]: Timestamp('2000-01-06 00:00:00')
要获得正确的 datetime.datetime
对象数组,请使用 to_pydatetime
方法:
In [11]: dt_array = rng.to_pydatetime()
In [12]: dt_array
Out[12]:
array([datetime.datetime(2000, 1, 1, 0, 0),
datetime.datetime(2000, 1, 2, 0, 0),
datetime.datetime(2000, 1, 3, 0, 0),
datetime.datetime(2000, 1, 4, 0, 0),
datetime.datetime(2000, 1, 5, 0, 0),
datetime.datetime(2000, 1, 6, 0, 0),
datetime.datetime(2000, 1, 7, 0, 0),
datetime.datetime(2000, 1, 8, 0, 0),
datetime.datetime(2000, 1, 9, 0, 0),
datetime.datetime(2000, 1, 10, 0, 0)], dtype=object)
In [13]: dt_array[5]
Out[13]: datetime.datetime(2000, 1, 6, 0, 0)
matplotlib 知道如何处理 datetime.datetime
,但不知道如何处理 Timestamp 对象。虽然我建议你使用 TimeSeries.plot
绘制时间序列,但你可以使用 to_pydatetime
或为 Timestamp 类型注册一个转换器。更多信息请参考matplotlib 文档。
警告
在 NumPy 1.6 版本中,用户界面 API 中的纳秒 datetime64 单元存在 bug。特别是,数组的字符串版本显示垃圾值,并且转换为 dtype=object
也同样存在问题。
In [14]: rng = pd.date_range("1/1/2000", periods=10)
In [15]: rng
Out[15]:
DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03', '2000-01-04',
'2000-01-05', '2000-01-06', '2000-01-07', '2000-01-08',
'2000-01-09', '2000-01-10'],
dtype='datetime64[ns]', freq='D')
In [16]: np.asarray(rng)
Out[16]:
array(['2000-01-01T00:00:00.000000000', '2000-01-02T00:00:00.000000000',
'2000-01-03T00:00:00.000000000', '2000-01-04T00:00:00.000000000',
'2000-01-05T00:00:00.000000000', '2000-01-06T00:00:00.000000000',
'2000-01-07T00:00:00.000000000', '2000-01-08T00:00:00.000000000',
'2000-01-09T00:00:00.000000000', '2000-01-10T00:00:00.000000000'],
dtype='datetime64[ns]')
In [17]: converted = np.asarray(rng, dtype=object)
In [18]: converted[5]
Out[18]: Timestamp('2000-01-06 00:00:00')
相信我:不要惊慌。如果你使用 NumPy 1.6 并且将与 datetime64
值的交互限制在 pandas 的 API 中,那么一切都会很好。数据类型没有问题(内部是一个 64 位整数);所有重要的数据处理都在 pandas 中进行,并且经过了严格测试。我强烈建议你不要直接在 NumPy 1.6 中使用 datetime64 数组,而只使用 pandas API。
支持非唯一索引:在后一种情况下,你可能有一段代码在 try:... catch:
块中失败,因为索引不是唯一的。在许多情况下,它将不再失败(一些方法如 append
仍然检查唯一性,除非禁用)。然而,一切都没有丢失:你可以检查 index.is_unique
,如果为 False
则显式引发异常,或者转到不同的代码分支。
贡献者
总共有 27 人为这个版本贡献了补丁。名字后面带有“+”的人第一次贡献了补丁。
-
Adam Klein
-
Chang She
-
David Zaslavsky +
-
Eric Chlebek +
-
Jacques Kvam
-
Kamil Kisiel
-
Kelsey Jordahl +
-
Kieran O’Mahony +
-
Lorenzo Bolla +
-
Luca Beltrame
-
Marc Abramowitz +
-
Mark Wiebe +
-
Paddy Mullen +
-
Peng Yu +
-
Roy Hyunjin Han +
-
RuiDC +
-
Senthil Palanisami +
-
Skipper Seabold
-
Stefan van der Walt +
-
Takafumi Arakaki +
-
Thomas Kluyver
-
Vytautas Jancauskas +
-
Wes McKinney
-
Wouter Overmeire
-
Yaroslav Halchenko
-
thuske +
-
timmie +
版本 0.7.3(2012 年 4 月 12 日)
这是从 0.7.2 的一个小版本发布,修复了许多小错误并添加了一些不错的新功能。还有一些需要注意的 API 变更;这些不应该影响很多用户,并且我们倾向于称它们为“错误修复”,尽管它们确实构成了行为上的变化。请查看完整的发布说明或 GitHub 上的问题跟踪器以获取完整列表。
新功能
-
新的固定宽度文件读取器,
read_fwf
-
用于制作散点图矩阵的新的 scatter_matrix 函数
from pandas.tools.plotting import scatter_matrix
scatter_matrix(df, alpha=0.2) # noqa F821
- 为 Series 和 DataFrame 的
plot
方法添加stacked
参数,用于堆叠条形图。
df.plot(kind="bar", stacked=True) # noqa F821
df.plot(kind="barh", stacked=True) # noqa F821
-
为
DataFrame.plot
和Series.plot
添加对数 x 和 y 缩放选项 -
为 Series 和 DataFrame 添加计算峰度的
kurt
方法
NA 布尔比较 API 变更
撤销了一些关于如何处理非数值 Series 中的 NA 值(通常表示为 NaN
或 None
)的更改:
In [1]: series = pd.Series(["Steve", np.nan, "Joe"])
In [2]: series == "Steve"
Out[2]:
0 True
1 False
2 False
Length: 3, dtype: bool
In [3]: series != "Steve"
Out[3]:
0 False
1 True
2 True
Length: 3, dtype: bool
在比较中,NA / NaN 除了 !=
为 True
外,始终会被视为 False
。在存在 NA 数据的情况下,对布尔运算要非常小心。如果担心这一点,您可能希望在布尔数组操作中添加一个明确的 NA 过滤器:
In [4]: mask = series == "Steve"
In [5]: series[mask & series.notnull()]
Out[5]:
0 Steve
Length: 1, dtype: object
尽管在比较中传播 NA 对某些用户来说可能是正确的行为(并且你可以纯粹从技术角度辩论这样做是正确的),但评估认为在所有地方传播 NA,包括在数值数组中,会给用户带来大量问题。因此,采取了“实用性胜过纯粹性”的方法。这个问题可能在未来的某个时候重新审视。
其他 API 变更
在对分组的 Series 调用 apply
时,返回值也将是一个 Series,以使其与 DataFrame 的 groupby
行为更加一致:
In [6]: df = pd.DataFrame(
...: {
...: "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"],
...: "B": ["one", "one", "two", "three", "two", "two", "one", "three"],
...: "C": np.random.randn(8),
...: "D": np.random.randn(8),
...: }
...: )
...:
In [7]: df
Out[7]:
A B C D
0 foo one 0.469112 -0.861849
1 bar one -0.282863 -2.104569
2 foo two -1.509059 -0.494929
3 bar three -1.135632 1.071804
4 foo two 1.212112 0.721555
5 bar two -0.173215 -0.706771
6 foo one 0.119209 -1.039575
7 foo three -1.044236 0.271860
[8 rows x 4 columns]
In [8]: grouped = df.groupby("A")["C"]
In [9]: grouped.describe()
Out[9]:
count mean std min 25% 50% 75% max
A
bar 3.0 -0.530570 0.526860 -1.135632 -0.709248 -0.282863 -0.228039 -0.173215
foo 5.0 -0.150572 1.113308 -1.509059 -1.044236 0.119209 0.469112 1.212112
[2 rows x 8 columns]
In [10]: grouped.apply(lambda x: x.sort_values()[-2:]) # top 2 values
Out[10]:
A
bar 1 -0.282863
5 -0.173215
foo 0 0.469112
4 1.212112
Name: C, Length: 4, dtype: float64
贡献者
总共有 15 人为这个版本贡献了补丁。名字后面带有“+”的人第一次贡献了补丁。
-
Abraham Flaxman +
-
Adam Klein
-
Andreas H. +
-
Chang She
-
Dieter Vandenbussche
-
Jacques Kvam +
-
K.-Michael Aye +
-
Kamil Kisiel +
-
Martin Blais +
-
Skipper Seabold
-
Thomas Kluyver
-
Wes McKinney
-
Wouter Overmeire
-
Yaroslav Halchenko
-
lgautier +
新功能
-
新的固定宽度文件读取器,
read_fwf
-
用于制作散点图矩阵的新的 scatter_matrix 函数
from pandas.tools.plotting import scatter_matrix
scatter_matrix(df, alpha=0.2) # noqa F821
- 为 Series 和 DataFrame 的
plot
方法添加stacked
参数,用于堆叠条形图。
df.plot(kind="bar", stacked=True) # noqa F821
df.plot(kind="barh", stacked=True) # noqa F821
-
为
DataFrame.plot
和Series.plot
添加对数 x 和 y 缩放选项 -
为 Series 和 DataFrame 添加了
kurt
方法来计算峰度
NA 布尔比较 API 更改
撤销了一些关于如何处理非数值 Series 中的 NA 值(通常表示为 NaN
或 None
)的更改:
In [1]: series = pd.Series(["Steve", np.nan, "Joe"])
In [2]: series == "Steve"
Out[2]:
0 True
1 False
2 False
Length: 3, dtype: bool
In [3]: series != "Steve"
Out[3]:
0 False
1 True
2 True
Length: 3, dtype: bool
在比较中,NA / NaN 除了 !=
为 True
外,始终会被视为 False
。在存在 NA 数据的情况下,一定要非常小心处理布尔运算,特别是否定。如果担心这一点,您可能希望在布尔数组操作中添加一个明确的 NA 过滤器:
In [4]: mask = series == "Steve"
In [5]: series[mask & series.notnull()]
Out[5]:
0 Steve
Length: 1, dtype: object
在比较中传播 NA 可能对一些用户来说是正确的行为(你可以从纯技术角度辩论这样做是正确的),但评估认为在所有地方传播 NA,包括在数值数组中,会给用户带来大量问题。因此,采取了“实用性胜过纯粹性”的方法。这个问题可能在未来的某个时候重新讨论。
其他 API 更改
在对分组 Series 调用 apply
时,返回值也将是一个 Series,以使其与 DataFrame 的 groupby
行为更一致:
In [6]: df = pd.DataFrame(
...: {
...: "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"],
...: "B": ["one", "one", "two", "three", "two", "two", "one", "three"],
...: "C": np.random.randn(8),
...: "D": np.random.randn(8),
...: }
...: )
...:
In [7]: df
Out[7]:
A B C D
0 foo one 0.469112 -0.861849
1 bar one -0.282863 -2.104569
2 foo two -1.509059 -0.494929
3 bar three -1.135632 1.071804
4 foo two 1.212112 0.721555
5 bar two -0.173215 -0.706771
6 foo one 0.119209 -1.039575
7 foo three -1.044236 0.271860
[8 rows x 4 columns]
In [8]: grouped = df.groupby("A")["C"]
In [9]: grouped.describe()
Out[9]:
count mean std min 25% 50% 75% max
A
bar 3.0 -0.530570 0.526860 -1.135632 -0.709248 -0.282863 -0.228039 -0.173215
foo 5.0 -0.150572 1.113308 -1.509059 -1.044236 0.119209 0.469112 1.212112
[2 rows x 8 columns]
In [10]: grouped.apply(lambda x: x.sort_values()[-2:]) # top 2 values
Out[10]:
A
bar 1 -0.282863
5 -0.173215
foo 0 0.469112
4 1.212112
Name: C, Length: 4, dtype: float64
贡献者
总共有 15 人为这个版本贡献了补丁。名字旁边带有“+”符号的人第一次贡献了补丁。
-
Abraham Flaxman +
-
Adam Klein
-
Andreas H. +
-
Chang She
-
Dieter Vandenbussche
-
Jacques Kvam +
-
K.-Michael Aye +
-
Kamil Kisiel +
-
Martin Blais +
-
Skipper Seabold
-
Thomas Kluyver
-
Wes McKinney
-
Wouter Overmeire
-
Yaroslav Halchenko
-
lgautier +
版本 0.7.2(2012 年 3 月 16 日)
此版本针对 0.7.1 中的错误,并添加了一些次要特性。
新特性
- 在 DataFrame.rank 中添加额外的打破平局方法(GH 874)
- 为 Series、DataFrame 添加升序参数以对排名进行排列(GH 875)
- 为 DataFrame.from_records 添加 coerce_float 选项(GH 893)
- 添加 sort_columns 参数以允许未排序的绘图(GH 918)
- 在 GroupBy 上通过属性启用列访问(GH 882)
- 可以将值字典传递给 DataFrame.fillna(GH 661)
- 可以通过在 .ix 中传递值列表来选择多个层次化组(GH 134)
- 为 DataFrame.fillna 添加
axis
选项(GH 174)- 为
drop
添加 level 关键字,用于从一个层级中删除值(GH 159)
性能改进
贡献者
总共有 12 人为此版本提供了补丁。带有 “+” 的人表示是首次为此版本贡献补丁。
-
Adam Klein
-
Benjamin Gross +
-
Dan Birken +
-
Dieter Vandenbussche
-
Josh +
-
Thomas Kluyver
-
Travis N. Vaught +
-
Wes McKinney
-
Wouter Overmeire
-
claudiobertoldi +
-
elpres +
-
joshuaar +
新特性
- 在 DataFrame.rank 中添加额外的打破平局方法(GH 874)
- 为 Series、DataFrame 添加升序参数以对排名进行排列(GH 875)
- 为 DataFrame.from_records 添加 coerce_float 选项(GH 893)
- 添加 sort_columns 参数以允许未排序的绘图(GH 918)
- 在 GroupBy 上通过属性启用列访问(GH 882)
- 可以将值字典传递给 DataFrame.fillna(GH 661)
- 可以通过在 .ix 中传递值列表来选择多个层次化组(GH 134)
- 为 DataFrame.fillna 添加
axis
选项(GH 174)- 为
drop
添加 level 关键字,用于从一个层级中删除值(GH 159)
性能改进
贡献者
总共有 12 人为此版本贡献了补丁。有“+”符号的人是首次贡献补丁的。
-
Adam Klein
-
Benjamin Gross +
-
Dan Birken +
-
Dieter Vandenbussche
-
Josh +
-
Thomas Kluyver
-
Travis N. Vaught +
-
Wes McKinney
-
Wouter Overmeire
-
claudiobertoldi +
-
elpres +
-
joshuaar +
版本 0.7.1(2012 年 2 月 29 日)
此版本包括一些新功能,并解决了 0.7.0 版中的十几个错误。
新功能
- 为 pandas 命名空间添加了
to_clipboard
函数,用于将对象写入系统剪贴板 (GH 774)- 为 DataFrame 添加
itertuples
方法,用于以元组形式迭代 DataFrame 的行 (GH 818)- 添加了将
fill_value
和method
传递给 DataFrame 和 Series 的align
方法的能力 (GH 806, GH 807)- 为
reindex
、align
方法添加了fill_value
选项 (GH 784)- 允许
concat
从 Series 生成 DataFrame (GH 787)- 为 Series 添加
between
方法 (GH 802)- 为 DataFrame 添加 HTML 表示钩子,用于 IPython HTML 笔记本 (GH 773)
- 支持使用 openpyxl 读取 Excel 2007 XML 文档
性能改进
- 改进了 DataFrame 上
fillna
的性能和内存使用- 可以沿着
axis=1
连接一系列 Series 以获得 DataFrame (GH 787)
贡献者
总共有 9 人为此版本贡献了补丁。名字后面带有“+”的人第一次贡献了补丁。
-
Adam Klein
-
Brian Granger +
-
Chang She
-
Dieter Vandenbussche
-
Josh Klein
-
Steve +
-
Wes McKinney
-
Wouter Overmeire
-
Yaroslav Halchenko
新功能
- 为 pandas 命名空间添加了
to_clipboard
函数,用于将对象写入系统剪贴板 (GH 774)- 为 DataFrame 添加
itertuples
方法,用于以元组���式迭代 DataFrame 的行 (GH 818)- 添加了将
fill_value
和method
传递给 DataFrame 和 Series 的align
方法的能力 (GH 806, GH 807)- 为
reindex
、align
方法添加了fill_value
选项 (GH 784)- 允许
concat
从 Series 生成 DataFrame (GH 787)- 为 Series 添加
between
方法 (GH 802)- 为 DataFrame 添加 HTML 表示钩子,用于 IPython HTML 笔记本 (GH 773)
- 支持使用 openpyxl 读取 Excel 2007 XML 文档
性能改进
- 改进了 DataFrame 上
fillna
的性能和内存使用- 可以沿着
axis=1
连接一系列 Series 以获得 DataFrame (GH 787)
贡献者
一共有 9 人为这个版本贡献了补丁。名字后面带有“+”符号的人是第一次贡献补丁的。
-
Adam Klein
-
Brian Granger +
-
Chang She
-
Dieter Vandenbussche
-
Josh Klein
-
Steve +
-
Wes McKinney
-
Wouter Overmeire
-
Yaroslav Halchenko
版本 0.7.0(2012 年 2 月 9 日)
新功能
-
新的统一的合并函数,可高效执行完整的数据库/关系代数操作。重构了现有的连接方法以使用新的基础设施,导致了显著的性能提升(GH 220,GH 249,GH 267)
-
新的统一连接函数,用于沿轴连接 Series、DataFrame 或 Panel 对象。可以形成其他轴的并集或交集。提高了
Series.append
和DataFrame.append
的性能(GH 468,GH 479,GH 273) -
可以将多个 DataFrame 传递给
DataFrame.append
来连接(堆叠),也可以将多个 Series 传递给Series.append
-
可以将字典列表(例如,JSON 对象列表)传递给 DataFrame 构造函数(GH 526)
-
现在可以通过
__getitem__
在 DataFrame 中设置多列,用于转换(GH 342) -
在
DataFrame.apply
中处理不同索引的输出值(GH 498)
In [1]: df = pd.DataFrame(np.random.randn(10, 4))
In [2]: df.apply(lambda x: x.describe())
Out[2]:
0 1 2 3
count 10.000000 10.000000 10.000000 10.000000
mean 0.190912 -0.395125 -0.731920 -0.403130
std 0.730951 0.813266 1.112016 0.961912
min -0.861849 -2.104569 -1.776904 -1.469388
25% -0.411391 -0.698728 -1.501401 -1.076610
50% 0.380863 -0.228039 -1.191943 -1.004091
75% 0.658444 0.057974 -0.034326 0.461706
max 1.212112 0.577046 1.643563 1.071804
[8 rows x 4 columns]
-
添加了
reorder_levels
方法到 Series 和 DataFrame(GH 534) -
添加了类似字典的
get
函数到 DataFrame 和 Panel(GH 521) -
添加了
DataFrame.iterrows
方法,用于高效地迭代 DataFrame 的行 -
添加了
DataFrame.to_panel
,代码改编自LongPanel.to_long
-
添加了
reindex_axis
方法到 DataFrame -
在
DataFrame
和Series
的二元算术函数上添加了level
选项 -
在 Series 和 DataFrame 的
reindex
和align
方法上添加了level
选项,用于在级别之间广播值(GH 542,GH 552,其他) -
为
Panel
添加了基于属性的项目访问,并添加了 IPython 完成(GH 563) -
为
Series.plot
添加了logy
选项,用于对 Y 轴进行对数缩放 -
添加
index
和header
选项到DataFrame.to_string
-
可以将多个 DataFrame 传递给
DataFrame.join
以在索引上进行连接(GH 115) -
可以将多个 Panel 传递给
Panel.join
(GH 115) -
添加
justify
参数到DataFrame.to_string
以允许不同的列标题对齐 -
添加
sort
选项到 GroupBy 以允许禁用对分组键进行排序以进行潜在的加速(GH 595) -
可以将 MaskedArray 传递给 Series 构造函数(GH 563)
-
通过属性和 IPython 完成添加面板项访问(GH 554)
-
实现
DataFrame.lookup
,用于检索给定行和列标签序列的值的花式索引类似物(GH 338) -
可以在 DataFrame 上通过传递函数列表进行分组聚合,产生具有分层列的聚合结果(GH 166)
-
可以在 Series 和 DataFrame 上调用
cummin
和cummax
以分别获取累积最小值和最大值(GH 647) -
value_range
添加为获取 DataFrame 的最小值和最大值的实用函数(GH 288) -
向
read_csv
、read_table
、to_csv
和from_csv
添加encoding
参数以处理非 ASCII 文本(GH 717) -
添加
abs
方法到 pandas 对象 -
添加
crosstab
函数以轻松计算频率表 -
添加
isin
方法到索引对象 -
添加
level
参数到 DataFrame 的xs
方法。
API 更改为整数索引
在 0.7.0 中,可能是最潜在风险的 API 更改之一,但也是最重要的之一,是关于如何处理整数索引与标签索引相关的完全审查。这是一个例子:
In [3]: s = pd.Series(np.random.randn(10), index=range(0, 20, 2))
In [4]: s
Out[4]:
0 -1.294524
2 0.413738
4 0.276662
6 -0.472035
8 -0.013960
10 -0.362543
12 -0.006154
14 -0.923061
16 0.895717
18 0.805244
Length: 10, dtype: float64
In [5]: s[0]
Out[5]: -1.2945235902555294
In [6]: s[2]
Out[6]: 0.41373810535784006
In [7]: s[4]
Out[7]: 0.2766617129497566
这一切都与之前的行为完全相同。但是,如果你要求一个不包含在 Series 中的键,在 0.6.1 版本及以前,Series 会回退到基于位置的查找。现在会引发KeyError
:
In [2]: s[1]
KeyError: 1
这个改变也对 DataFrame 产生了同样的影响:
In [3]: df = pd.DataFrame(np.random.randn(8, 4), index=range(0, 16, 2))
In [4]: df
0 1 2 3
0 0.88427 0.3363 -0.1787 0.03162
2 0.14451 -0.1415 0.2504 0.58374
4 -1.44779 -0.9186 -1.4996 0.27163
6 -0.26598 -2.4184 -0.2658 0.11503
8 -0.58776 0.3144 -0.8566 0.61941
10 0.10940 -0.7175 -1.0108 0.47990
12 -1.16919 -0.3087 -0.6049 -0.43544
14 -0.07337 0.3410 0.0424 -0.16037
In [5]: df.ix[3]
KeyError: 3
为了支持纯整数索引,以下方法已被添加:
方法 | 描述 |
---|---|
Series.iget_value(i) |
检索存储在位置i 的值 |
Series.iget(i) |
iget_value 的别名 |
DataFrame.irow(i) |
检索第i 行 |
DataFrame.icol(j) |
检索第j 列 |
DataFrame.iget_value(i, j) |
检索第i 行和第j 列的值 |
关于基于标签的切片的 API 调整
使用ix
进行基于标签的切片现在要求索引已排序(单调递增)除非起始点和终点都包含在索引中:
In [1]: s = pd.Series(np.random.randn(6), index=list('gmkaec'))
In [2]: s
Out[2]:
g -1.182230
m -0.276183
k -0.243550
a 1.628992
e 0.073308
c -0.539890
dtype: float64
那么这是可以的:
In [3]: s.ix['k':'e']
Out[3]:
k -0.243550
a 1.628992
e 0.073308
dtype: float64
但这不是:
In [12]: s.ix['b':'h']
KeyError 'b'
如果索引已排序,则“范围选择”将是可能的:
In [4]: s2 = s.sort_index()
In [5]: s2
Out[5]:
a 1.628992
c -0.539890
e 0.073308
g -1.182230
k -0.243550
m -0.276183
dtype: float64
In [6]: s2.ix['b':'h']
Out[6]:
c -0.539890
e 0.073308
g -1.182230
dtype: float64
Series []
操作符的更改
作为一种符号方便,当通过[]
(即__getitem__
和__setitem__
方法)获取和设置值时,可以传递标签序列或标签切片给 Series。行为将与将类似输入传递给ix
时相同除了在整数索引的情况下:
In [8]: s = pd.Series(np.random.randn(6), index=list('acegkm'))
In [9]: s
Out[9]:
a -1.206412
c 2.565646
e 1.431256
g 1.340309
k -1.170299
m -0.226169
Length: 6, dtype: float64
In [10]: s[['m', 'a', 'c', 'e']]
Out[10]:
m -0.226169
a -1.206412
c 2.565646
e 1.431256
Length: 4, dtype: float64
In [11]: s['b':'l']
Out[11]:
c 2.565646
e 1.431256
g 1.340309
k -1.170299
Length: 4, dtype: float64
In [12]: s['c':'k']
Out[12]:
c 2.565646
e 1.431256
g 1.340309
k -1.170299
Length: 4, dtype: float64
对于整数索引的情况,行为将与以前完全相同(遮蔽ndarray
):
In [13]: s = pd.Series(np.random.randn(6), index=range(0, 12, 2))
In [14]: s[[4, 0, 2]]
Out[14]:
4 0.132003
0 0.410835
2 0.813850
Length: 3, dtype: float64
In [15]: s[1:5]
Out[15]:
2 0.813850
4 0.132003
6 -0.827317
8 -0.076467
Length: 4, dtype: float64
如果希望在具有标签语义的整数索引上使用序列和切片进行索引,请使用ix
。
其他 API 更改
-
已完全移除弃用的
LongPanel
类 -
如果在 DataFrame 的列上调用
Series.sort
,现在将引发异常。之前可能会意外通过df[col].sort()
而不是无副作用的方法df[col].order()
来改变 DataFrame 的列(GH 316) -
各种重命名和弃用,将(无害地)引发
FutureWarning
-
drop
作为DataFrame.reset_index
的可选参数添加(GH 699)
性能改进
-
Cython 化的 GroupBy 聚合不再预先排序数据,从而实现了显著的加速(GH 93)。通过在 Cython 中巧妙操作 ndarray 数据类型,使用 Python 函数的 GroupBy 聚合显著加快了速度(GH 496)。
-
在 DataFrame 构造函数中,当传递的列标签与数据不匹配时,提供更好的错误消息(GH 497)
-
当传递 Python 函数时,通过在 Cython 中重复使用 ndarray 对象,大幅提高了多 GroupBy 聚合的性能(GH 496)
-
可以在 HDFStore 中存储由元组和浮点数索引的对象(GH 492)
-
在 Series.to_string 中默认不打印长度,添加
length
选项(GH 489) -
改进 Cython 代码,使多 GroupBy 聚合无需对数据进行排序(GH 93)
-
通过在 MultiIndex 中存储元组来提高重新索引速度,测试向后反序列化兼容性
-
通过使用专门的 Cython take 函数提高列重新索引性能
-
进一步调整 Series.getitem 的性能以适应标准用例
-
避免在某些情况下创建索引字典(即在获取切片等情况下),这是之前版本的退化
-
如果未安装 NumPy,则在 setup.py 中提供更友好的错误消息
-
在 Panel 类中也使用一组通用的 NA 处理操作(sum、mean 等)(GH 536)
-
在对具有常规(非分层)索引的 DataFrame 调用
reset_index
时默认进行名称分配(GH 476) -
在 Series/DataFrame 统计操作中尽可能使用 Cython 化的分组器,传递
level
参数(GH 545) -
将 skiplist 数据结构移植到 C 以加速大多数典型用例中的
rolling_median
处理速度约 5-10 倍(GH 374)
贡献者
共有 18 人为此版本提交了补丁。带有“+”符号的人员首次为此贡献了补丁。
-
Adam Klein
-
Bayle Shanks +
-
Chris Billington +
-
Dieter Vandenbussche
-
Fabrizio Pollastri +
-
Graham Taylor +
-
Gregg Lind +
-
Josh Klein +
-
Luca Beltrame
-
Olivier Grisel +
-
Skipper Seabold
-
Thomas Kluyver
-
Thomas Wiecki +
-
Wes McKinney
-
Wouter Overmeire
-
Yaroslav Halchenko
-
fabriziop +
-
theandygross +
新特性
-
新的统一的合并函数,用于有效执行完整的数据库/关系代数操作。重构现有的连接方法以使用新的基础架构,从而实现了可观的性能提升(GH 220,GH 249,GH 267)
-
新的统一连接函数,用于沿轴连接 Series、DataFrame 或 Panel 对象。可以形成其他轴的并集或交集。提高了
Series.append
和DataFrame.append
的性能(GH 468,GH 479,GH 273) -
可以将多个 DataFrame 传递给
DataFrame.append
进行连接(堆叠),也可以将多个 Series 传递给Series.append
-
可以将字典列表(例如,JSON 对象列表)传递给 DataFrame 构造函数(GH 526)
-
现在可以通过
__getitem__
在 DataFrame 中设置多列,对于转换很有用(GH 342) -
在
DataFrame.apply
中处理不同索引的输出值(GH 498)
In [1]: df = pd.DataFrame(np.random.randn(10, 4))
In [2]: df.apply(lambda x: x.describe())
Out[2]:
0 1 2 3
count 10.000000 10.000000 10.000000 10.000000
mean 0.190912 -0.395125 -0.731920 -0.403130
std 0.730951 0.813266 1.112016 0.961912
min -0.861849 -2.104569 -1.776904 -1.469388
25% -0.411391 -0.698728 -1.501401 -1.076610
50% 0.380863 -0.228039 -1.191943 -1.004091
75% 0.658444 0.057974 -0.034326 0.461706
max 1.212112 0.577046 1.643563 1.071804
[8 rows x 4 columns]
-
新增
reorder_levels
方法到 Series 和 DataFrame (GH 534) -
在 DataFrame 和 Panel 上添加类似字典的
get
函数 (GH 521) -
新增
DataFrame.iterrows
方法,用于高效地遍历 DataFrame 的行 -
新增
DataFrame.to_panel
方法,代码改编自LongPanel.to_long
-
新增
reindex_axis
方法添加到 DataFrame -
新增
level
选项到DataFrame
和Series
的二进制算术函数 -
在 Series 和 DataFrame 的
reindex
和align
方法上添加level
选项,用于在级别之间广播值 (GH 542, GH 552, 等) -
添加基于属性的项访问到
Panel
并添加 IPython 自动完成功能 (GH 563) -
新增
logy
选项到Series.plot
以在 Y 轴上进行对数缩放 -
新增
index
和header
选项到DataFrame.to_string
-
可以将多个 DataFrames 传递给
DataFrame.join
来按索引进行连接 (GH 115) -
可以将多个 Panels 传递给
Panel.join
(GH 115) -
添加
justify
参数到DataFrame.to_string
,以允许不同的列标题对齐方式 -
在 GroupBy 上添加
sort
选项,以允许禁用对分组键的排序,以提高速度 (GH 595) -
可以将 MaskedArray 传递给 Series 构造函数 (GH 563)
-
通过属性和 IPython 自动完成功能访问 Panel 的项 (GH 554)
-
实现
DataFrame.lookup
,用于检索给定一系列行和列标签的值的 fancy-indexing 类似物 (GH 338) -
可以将函数列表传递给 DataFrame 的 groupby 聚合,生成具有分层列的聚合结果 (GH 166)
-
可以在 Series 和 DataFrame 上调用
cummin
和cummax
来分别获取累积最小值和最大值 (GH 647) -
已添加
value_range
作为实用函数以获取 DataFrame 的最小值和最大值(GH 288) -
对于非 ASCII 文本,已添加
encoding
参数到read_csv
、read_table
、to_csv
和from_csv
(GH 717) -
已添加
abs
方法到 pandas 对象 -
已添加
crosstab
函数以轻松计算频率表 -
已添加
isin
方法到索引对象 -
已添加
level
参数到 DataFrame 的xs
方法中。
API 更改以支持整数索引
在 0.7.0 中潜在风险最高的 API 更改之一,但也是最重要的之一,是完全审查了关于整数索引如何处理与基于标签的索引相关的内容。这里是一个例子:
In [3]: s = pd.Series(np.random.randn(10), index=range(0, 20, 2))
In [4]: s
Out[4]:
0 -1.294524
2 0.413738
4 0.276662
6 -0.472035
8 -0.013960
10 -0.362543
12 -0.006154
14 -0.923061
16 0.895717
18 0.805244
Length: 10, dtype: float64
In [5]: s[0]
Out[5]: -1.2945235902555294
In [6]: s[2]
Out[6]: 0.41373810535784006
In [7]: s[4]
Out[7]: 0.2766617129497566
所有这一切都与之前的行为完全相同。但是,如果你要求的键不包含在 Series 中,在 0.6.1 版本及之前的版本中,Series 会回退到基于位置的查找。现在会引发 KeyError
:
In [2]: s[1]
KeyError: 1
这个变化也会对 DataFrame 产生相同的影响:
In [3]: df = pd.DataFrame(np.random.randn(8, 4), index=range(0, 16, 2))
In [4]: df
0 1 2 3
0 0.88427 0.3363 -0.1787 0.03162
2 0.14451 -0.1415 0.2504 0.58374
4 -1.44779 -0.9186 -1.4996 0.27163
6 -0.26598 -2.4184 -0.2658 0.11503
8 -0.58776 0.3144 -0.8566 0.61941
10 0.10940 -0.7175 -1.0108 0.47990
12 -1.16919 -0.3087 -0.6049 -0.43544
14 -0.07337 0.3410 0.0424 -0.16037
In [5]: df.ix[3]
KeyError: 3
为了支持纯整数索引,已添加以下方法:
方法 | 描述 |
---|---|
Series.iget_value(i) |
检索存储在位置 i 处的值 |
Series.iget(i) |
iget_value 的别名 |
DataFrame.irow(i) |
检索第 i 行 |
DataFrame.icol(j) |
检索第 j 列 |
DataFrame.iget_value(i, j) |
检索第 i 行和第 j 列的值 |
关于基于标签的切片的 API 调整
使用 ix
进行基于标签的切片现在要求索引已排序(单调)除非起始点和终点都包含在索引中:
In [1]: s = pd.Series(np.random.randn(6), index=list('gmkaec'))
In [2]: s
Out[2]:
g -1.182230
m -0.276183
k -0.243550
a 1.628992
e 0.073308
c -0.539890
dtype: float64
这样是可以的:
In [3]: s.ix['k':'e']
Out[3]:
k -0.243550
a 1.628992
e 0.073308
dtype: float64
但这样不行:
In [12]: s.ix['b':'h']
KeyError 'b'
如果索引已排序,将会出现“范围选择”:
In [4]: s2 = s.sort_index()
In [5]: s2
Out[5]:
a 1.628992
c -0.539890
e 0.073308
g -1.182230
k -0.243550
m -0.276183
dtype: float64
In [6]: s2.ix['b':'h']
Out[6]:
c -0.539890
e 0.073308
g -1.182230
dtype: float64
Series []
运算符的更改
作为一种简便的表示方法,在通过 []
(即 __getitem__
和 __setitem__
方法)获取和设置 Series 的值时,你可以传递一个标签序列或标签切片到 Series。行为将与传递类似输入到 ix
除了整数索引的情况下一样:
In [8]: s = pd.Series(np.random.randn(6), index=list('acegkm'))
In [9]: s
Out[9]:
a -1.206412
c 2.565646
e 1.431256
g 1.340309
k -1.170299
m -0.226169
Length: 6, dtype: float64
In [10]: s[['m', 'a', 'c', 'e']]
Out[10]:
m -0.226169
a -1.206412
c 2.565646
e 1.431256
Length: 4, dtype: float64
In [11]: s['b':'l']
Out[11]:
c 2.565646
e 1.431256
g 1.340309
k -1.170299
Length: 4, dtype: float64
In [12]: s['c':'k']
Out[12]:
c 2.565646
e 1.431256
g 1.340309
k -1.170299
Length: 4, dtype: float64
对于整数索引的情况,行为将与以前完全相同(模糊化 ndarray
):
In [13]: s = pd.Series(np.random.randn(6), index=range(0, 12, 2))
In [14]: s[[4, 0, 2]]
Out[14]:
4 0.132003
0 0.410835
2 0.813850
Length: 3, dtype: float64
In [15]: s[1:5]
Out[15]:
2 0.813850
4 0.132003
6 -0.827317
8 -0.076467
Length: 4, dtype: float64
如果你希望在具有标签语义的整数索引上对序列进行索引和切片,使用 ix
。
其他 API 更改
-
已完全删除了弃用的
LongPanel
类 -
如果在 DataFrame 的列上调用
Series.sort
,现在将会引发异常。之前可能会通过df[col].sort()
而不是无副作用的方法df[col].order()
来意外改变 DataFrame 的列(GH 316) -
杂项重命名和弃用,这将(无害地)引发
FutureWarning
-
将
drop
添加为DataFrame.reset_index
的可选参数(GH 699)
性能改进
-
Cython 化的 GroupBy 聚合不再预先对数据进行排序,从而实现了显著的加速(GH 93)。通过在 Cython 中巧妙地操作 ndarray 数据类型,使用 Python 函数的 GroupBy 聚合显著加快了速度(GH 496)。
-
在 DataFrame 构造函数中传递的列标签与数据不匹配时,改进更好的错误消息(GH 497)
-
当传递 Python 函数时,显著改善多 GroupBy 聚合的性能,重用 Cython 中的 ndarray 对象(GH 496)
-
可以在 HDFStore 中存储由元组和浮点数索引的对象(GH 492)
-
默认情况下不在 Series.to_string 中打印长度,添加
length
选项(GH 489) -
改进 Cython 代码,使多组聚合无需对数据进行排序即可聚合(GH 93)
-
通过在 MultiIndex 中存储元组来提高 MultiIndex 重新索引速度,测试向后不兼容的反序列化兼容性
-
通过使用专门的 Cython take 函数提高列重新索引性能
-
进一步调整 Series.getitem 以适应标准用例
-
在某些情况下避免创建 Index 字典(即在获取切片等情况下),这是之前版本的回归
-
如果 NumPy 未安装,则在 setup.py 中提供更友好的错误消息
-
在 Panel 类中也使用一组常见的 NA 处理操作(sum、mean 等)(GH 536)
-
在 DataFrame 上调用
reset_index
时,默认名称分配为常规(非分层)索引时(GH 476) -
在传递
level
参数的情况下,在 Series/DataFrame stat 操作中尽可能使用 Cython 化的 groupers(GH 545) -
将 skiplist 数据结构移植到 C 中,以在大多数典型用例中将
rolling_median
的速度提高约 5-10 倍(GH 374)
贡献者
总共有 18 人为此版本贡献了补丁。名字后面带有“+”的人第一次贡献了补丁。
-
Adam Klein
-
Bayle Shanks +
-
Chris Billington +
-
Dieter Vandenbussche
-
Fabrizio Pollastri +
-
Graham Taylor +
-
Gregg Lind +
-
Josh Klein +
-
Luca Beltrame
-
Olivier Grisel +
-
Skipper Seabold
-
Thomas Kluyver
-
Thomas Wiecki +
-
Wes McKinney
-
Wouter Overmeire
-
Yaroslav Halchenko
-
fabriziop +
-
theandygross +
版本 0.6.1(2011 年 12 月 13 日)
新功能
-
可以将单行(作为 Series)追加到 DataFrame
-
为 Series.corr 和 DataFrame.corr 添加 Spearman 和 Kendall 等级相关选项(GH 428)
-
为 Series、DataFrame 和 Panel 添加了
get_value
和set_value
方法,用于对标量元素进行非常低开销的访问(在许多情况下快 2 倍以上)(GH 437,GH 438)。set_value
能够生成一个扩大的对象。 -
在沙盒中添加 PyQt 表格小部件(GH 435)
-
DataFrame.align 可以接受 Series 参数和一个轴选项(链接)(GH 461)
-
实现新的 SparseArray 和
SparseList
数据结构。SparseSeries 现在派生自 SparseArray(GH 463) -
更好的控制台打印选项(链接)(GH 453)
-
为 Series 和 DataFrame 实现快速数据排名,scipy.stats.rankdata 的快速版本(GH 428)
-
实现
DataFrame.from_items
替代构造函数(GH 444) -
DataFrame.convert_objects 方法用于推断对象列的更好数据类型(链接)(GH 302)
-
添加 rolling_corr_pairwise 函数用于计算相关矩阵的面板(链接)(GH 189)
-
添加 margins 选项到 pivot_table 用于计算子组聚合(链接)(GH 114)
-
添加
Series.from_csv
函数(GH 482) -
可以将 DataFrame/DataFrame 和 DataFrame/Series 传递给 rolling_corr/rolling_cov(GH #462)
-
MultiIndex.get_level_values 可以接受级别名称(链接)
性能改进
-
改进
DataFrame.describe
的内存使用(不必要地复制数据)(PR #425) -
在 Series 和 DataFrame 中一般情况下标量值查找的性能提高了 25% 或更多
-
修复 DataFrame 中横截面计数的性能回归,影响 DataFrame.dropna 的速度
-
DataFrame 中的列删除不复制数据(在块上计算视图)(GH #158)
贡献者
总共有 7 人为此版本贡献了补丁。名字后面带有“+”的人第一次贡献了补丁。
-
Dieter Vandenbussche
-
Fernando Perez +
-
Jev Kuznetsov +
-
Joon Ro
-
Ralph Bean +
-
Wes McKinney
-
Wouter Overmeire
新功能
-
可以将单行(作为 Series)附加到 DataFrame
-
为 Series.corr 和 DataFrame.corr 添加 Spearman 和 Kendall 排名相关选项(GH 428)
-
为 Series、DataFrame 和 Panel 添加
get_value
和set_value
方法,用于对标量元素进行非常低开销的访问(在许多情况下提高了 2 倍以上的速度)(GH 437,GH 438)。set_value
能够生成一个扩展对象。 -
将 PyQt 表格小部件添加到沙盒中(GH 435)
-
DataFrame.align 可以接受 Series 参数和一个轴选项(GH 461)
-
实现新的 SparseArray 和
SparseList
数据结构。SparseSeries 现在派生自 SparseArray(GH 463) -
更好的控制台打印选项(GH 453)
-
为 Series 和 DataFrame 实现快速数据排名,scipy.stats.rankdata 的快速版本(GH 428)
-
实现
DataFrame.from_items
替代构造函数(GH 444) -
DataFrame.convert_objects 方法用于推断对象列的更好数据类型(GH 302)
-
为计算相关矩阵的 Panel 添加 rolling_corr_pairwise 函数(GH 189)
-
为 pivot_table 添加 margins 选项,用于计算子组聚合(GH 114)
-
添加
Series.from_csv
函数(GH 482) -
可以将 DataFrame/DataFrame 和 DataFrame/Series 传递给 rolling_corr/rolling_cov(GH #462)
-
MultiIndex.get_level_values 可以接受级别名称
性能改进
-
改进
DataFrame.describe
的内存使用(不必要地复制数据)(PR #425) -
在 Series 和 DataFrame 中,优化一般情况下标量值查找的效率提高 25% 或更多
-
修复了 DataFrame 中横截面计数的性能回归问题,影响了 DataFrame.dropna 的速度
-
删除 DataFrame 中的列不会复制数据(在块上计算视图)(GH #158)
贡献者
总共有 7 人为这个版本贡献了补丁。名字后面带有“+”的人是第一次贡献补丁。
-
Dieter Vandenbussche
-
Fernando Perez +
-
Jev Kuznetsov +
-
Joon Ro
-
Ralph Bean +
-
Wes McKinney
-
Wouter Overmeire
版本 0.6.0(2011 年 11 月 25 日)
新特性
-
向
pandas.core.reshape
添加了melt
函数 -
在 Series 和 DataFrame 描述性统计中,添加了
level
参数,用于按级别分组(GH 313) -
向 Series 添加了
head
和tail
方法,类似于 DataFrame(GH 296) -
添加
Series.isin
函数,用于检查每个值是否包含在传递的序列中(GH 289) -
向
Series.to_string
添加了float_format
选项 -
向
read_csv
和read_table
添加了skip_footer
(GH 291) 和converters
(GH 343)选项 -
添加了用于删除重复 DataFrame 行并检查重复行的
drop_duplicates
和duplicated
函数(GH 319) -
在 DataFrame 上实现了 ‘&’, ‘|’, ‘^’, ‘-’ 运算符(GH 347)
-
添加了
Series.mad
,均值绝对偏差 -
向
QuarterEnd
DateOffset 添加了QuarterEnd
(GH 321) -
向 DataFrame 添加了
dot
(GH 65) -
向
DataFrame.from_dict
添加了orient
选项 -
向
DataFrame.from_records
添加了传递元组列表或列表列表的选项(GH 357) -
向 groupby 添加了多个级别(GH 103)
-
向 DataFrame 添加了快速
get_value
和put_value
方法(GH 360) -
在
DataFrame.plot
中添加了kind='bar'
选项(GH 348)(点击查看) -
在 Series 和 DataFrame 中添加了
idxmin
和idxmax
方法(GH 286)(点击查看) -
添加了从剪贴板解析 DataFrame 的
read_clipboard
方法(GH 300)(点击查看) -
为 Series 添加了计算唯一元素个数的
nunique
方法(GH 297)(点击查看) -
如果没有传递列名,则使 DataFrame 构造函数使用 Series 名称(GH 373)(点击查看)
-
在 read_table/read_csv 中支持正则表达式(GH 364)(点击查看)
-
添加了将 DataFrame 写入 HTML 的
DataFrame.to_html
方法(GH 387)(点击查看) -
在 DataFrame 中添加了对 MaskedArray 数据的支持,屏蔽的值转换为 NaN(GH 396)(点击查看)
-
添加了
DataFrame.boxplot
方法(GH 368)(点击查看) -
可以将额外的参数和关键字参数传递给 DataFrame.apply(GH 376)(点击查看)
-
用矢量
on
参数实现了DataFrame.join
方法(GH 312)(点击查看) -
向
DataFrame.plot
添加了legend
布尔标志(GH 324)(点击查看) -
可以将多个级别传递给
stack
和unstack
(GH 370)(点击查看) -
可以将多个值列传递给
pivot_table
(GH 381)(点击查看) -
在 GroupBy 中使用 Series 名称作为结果索引(GH 363)(点击查看)
-
为
DataFrame.apply
添加了raw
选项,用于仅需要 ndarray 时的性能优化(GH 309)(点击查看) -
向标准和面板 OLS 添加了适当且经过测试的加权最小二乘法(GH 303)
性能增强
-
VBENCH 对
cache_readonly
进行了 Cython 化处理,在整个代码库中获得了实质性的微性能增强 (GH 361) -
VBENCH 专用的 Cython 矩阵迭代器,用于应用任意缩减操作,比
np.apply_along_axis
快 3-5 倍 (GH 309) -
VBENCH 提高了
MultiIndex.from_tuples
的性能 -
VBENCH 专用的 Cython 矩阵迭代器,用于应用任意缩减操作
-
VBENCH + DOCUMENT 为
DataFrame.apply
添加了raw
选项,以获得更好的性能 -
VBENCH Series 和 DataFrame 中按级别进行的计数速度更快的 Cython 化处理 (GH 341)
-
VBENCH?在具有多个键且具有许多“空”组合的轴上,GroupBy 的性能显着提升
-
VBENCH 新的 Cython 向量化函数
map_infer
在传递逐元素 Python 函数时显着加快了Series.apply
和Series.map
的速度,灵感来源于 (GH 355) -
VBENCH 显著提高了
Series.order
的性能,这也使得对 Series 调用 np.unique 的速度更快 (GH 327) -
VBENCH 在具有 MultiIndex 的轴上,GroupBy 的性能大幅提升 (GH 299)
贡献者
共有 8 人对此版本做出了贡献。带有“+”的人第一次为此版本贡献了补丁。
-
Adam Klein +
-
Chang She +
-
Dieter Vandenbussche
-
Jeff Hammerbacher +
-
Nathan Pinger +
-
Thomas Kluyver
-
Wes McKinney
-
Wouter Overmeire +
新功能
-
添加了
melt
函数到pandas.core.reshape
-
添加了
level
参数到 Series 和 DataFrame 描述性统计的分组,用于按级别分组 (GH 313) -
添加了
head
和tail
方法到 Series,类似于 DataFrame (GH 296) -
添加了
Series.isin
函数,用于检查每个值是否包含在传递的序列中 (GH 289) -
添加了
float_format
选项到Series.to_string
-
添加了
skip_footer
(GH 291) 和converters
(GH 343) 选项到read_csv
和read_table
-
添加了
drop_duplicates
和duplicated
函数,用于删除重复的 DataFrame 行和检查重复行,分别是 (GH 319) -
在 DataFrame 上实现了运算符‘&’, ‘|’, ‘^’, ‘-’ (GH 347) (链接)
-
添加了
Series.mad
,均值绝对偏差 (链接) -
添加了
QuarterEnd
DateOffset (GH 321) (链接) -
为 DataFrame 添加了
dot
方法 (GH 65) (链接) -
在
DataFrame.from_dict
中添加了orient
选项 (链接) -
允许将元组列表或列表列表传递给
DataFrame.from_records
(GH 357) (链接) -
为 groupby 添加了多个级别 (GH 103) (链接)
-
为 DataFrame 添加了快速的
get_value
和put_value
方法 (GH 360) (链接) -
在
DataFrame.plot
中添加了kind='bar'
选项 (GH 348) (链接) -
为 Series 和 DataFrame 添加了
idxmin
和idxmax
(GH 286) (链接) -
添加了
read_clipboard
函数以从剪贴板解析 DataFrame (GH 300) (链接) -
为 Series 添加了
nunique
函数,用于计算唯一元素的数量 (GH 297) (链接) -
如果没有传递列名,则使 DataFrame 构造函数使用 Series 名称 (GH 373) (链接)
-
在 read_table/read_csv 中支持正则表达式 (GH 364) (链接)
-
添加了
DataFrame.to_html
以将 DataFrame 写入 HTML (GH 387) (链接) -
增加了对 DataFrame 中 MaskedArray 数据的支持,屏蔽值转换为 NaN(GH 396)
-
添加了
DataFrame.boxplot
函数(GH 368) -
可以向 DataFrame.apply 传递额外的 args、kwds(GH 376)
-
实现了带有向量
on
参数的DataFrame.join
(GH 312) -
为
DataFrame.plot
添加了legend
布尔标志(GH 324) -
可以将多个级别传递给
stack
和unstack
(GH 370) -
可以将多个值列传递给
pivot_table
(GH 381) -
在 GroupBy 中使用 Series 名称作为结果索引(GH 363)
-
为
DataFrame.apply
添加了raw
选项,仅需要 ndarray 时提供性能(GH 309) -
为标准和面板 OLS 添加了适当的、经过测试的加权最小二乘法(GH 303)
性能增强
-
VBENCH 将
cache_readonly
进行了 Cython 化处理,在整个代码库中获得了实质性的微小性能提升(GH 361) -
VBENCH 为应用任意缩减操作提供了特殊的 Cython 矩阵迭代器,性能比
np.apply_along_axis
提高了 3-5 倍(GH 309) -
VBENCH 改进了
MultiIndex.from_tuples
的性能 -
VBENCH 为应用任意缩减操作添加了特殊的 Cython 矩阵迭代器
-
VBENCH + DOCUMENT 为
DataFrame.apply
添加了raw
选项,以获得更好的性能 -
VBENCH 在 Series 和 DataFrame 中按级别进行了更快的 Cython 化计数(GH 341)
-
VBENCH? 在具有多个键的 GroupBy 中,对许多“空”组合进行了显著性能增强
-
VBENCH 通过添加新的 Cython 向量化函数
map_infer
,在传递元素级 Python 函数时显著加速了Series.apply
和Series.map
,灵感来源于(GH 355) -
VBENCH 大幅改进了
Series.order
的性能,这也使得在 Series 上调用 np.unique 更快了(GH 327) -
VBENCH 大幅提高了具有 MultiIndex 的轴上的 GroupBy 的性能(GH 299)
贡献者
总共有 8 人为这个版本贡献了补丁。带有 “+” 标记的人是第一次贡献补丁的。
-
Adam Klein +
-
Chang She +
-
Dieter Vandenbussche
-
Jeff Hammerbacher +
-
Nathan Pinger +
-
Thomas Kluyver
-
Wes McKinney
-
Wouter Overmeire +
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
2021-06-24 如何评价「施一公请辞清华大学副校长,全职执掌西湖大学」?你如何看待西湖大学的发展前景?