版本 0.17.1 (2015年11月21日)#

注意

我们荣幸地宣布,pandas 已成为 (NumFOCUS 组织) 的赞助项目。这将有助于确保 pandas 作为世界一流开源项目取得成功。

这是 0.17.0 的一个小型错误修复版本,包含大量错误修复以及一些新功能、增强功能和性能改进。我们建议所有用户升级到此版本。

主要亮点包括

  • 支持条件 HTML 格式化,详情见 此处

  • 在 csv 阅读器及其他操作中释放 GIL,详情见 此处

  • 修复了 0.16.2 版本中 DataFrame.drop_duplicates 的回归错误,该错误导致整数值产生不正确的结果 (GH 11376)

新功能#

条件 HTML 格式化#

警告

这是一项新功能,目前正在积极开发中。我们将在未来版本中添加更多功能,并可能引入破坏性变更。欢迎在 GH 11610 中提供反馈

我们增加了对条件 HTML 格式化的**实验性**支持:根据数据对 DataFrame 进行视觉样式设置。样式设置通过 HTML 和 CSS 实现。可以使用 pandas.DataFrame.style 属性访问 styler 类,该属性是附带您数据的 Styler 实例。

这里有一个快速示例

In [1]: np.random.seed(123)

In [2]: df = pd.DataFrame(np.random.randn(10, 5), columns=list("abcde"))

In [3]: html = df.style.background_gradient(cmap="viridis", low=0.5)

我们可以渲染 HTML 以获得下表。

abcde
0 -1.085631 0.997345 0.282978 -1.506295 -0.5786
1 1.651437 -2.426679 -0.428913 1.265936 -0.86674
2 -0.678886 -0.094709 1.49139 -0.638902 -0.443982
3 -0.434351 2.20593 2.186786 1.004054 0.386186
4 0.737369 1.490732 -0.935834 1.175829 -1.253881
5 -0.637752 0.907105 -1.428681 -0.140069 -0.861755
6 -0.255619 -2.798589 -1.771533 -0.699877 0.927462
7 -0.173636 0.002846 0.688223 -0.879536 0.283627
8 -0.805367 -1.727669 -0.3909 0.573806 0.338589
9 -0.01183 2.392365 0.412912 0.978736 2.238143

Styler 与 Jupyter Notebook 良好地交互。更多信息请参阅文档

增强功能#

  • DatetimeIndex 现在支持使用 astype(str) 转换为字符串 (GH 10442)

  • pandas.DataFrame.to_csv() 现在支持 compression (gzip/bz2) 参数 (GH 7615)

  • pd.read_* 函数现在也可以接受 pathlib.Pathpy:py._path.local.LocalPath 对象作为 filepath_or_buffer 参数 (GH 11033) - DataFrameSeries.to_csv().to_html().to_latex() 函数现在可以处理以波浪号开头的路径(例如 ~/Documents/)(GH 11438)

  • 如果未提供列名,DataFrame 现在将使用 namedtuple 的字段作为列 (GH 11181)

  • DataFrame.itertuples() 在可能的情况下现在返回 namedtuple 对象 (GH 11269, GH 11625)

  • 在平行坐标图中添加了 axvlines_kwds 参数 (GH 10709)

  • .info().memory_usage() 添加了选项,以提供内存消耗的深度内省。请注意,计算此选项可能非常耗时,因此是一个可选参数 (GH 11595)

    In [4]: df = pd.DataFrame({"A": ["foo"] * 1000})  # noqa: F821
    
    In [5]: df["B"] = df["A"].astype("category")
    
    # shows the '+' as we have object dtypes
    In [6]: df.info()
    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 1000 entries, 0 to 999
    Data columns (total 2 columns):
     #   Column  Non-Null Count  Dtype   
    ---  ------  --------------  -----   
     0   A       1000 non-null   object  
     1   B       1000 non-null   category
    dtypes: category(1), object(1)
    memory usage: 9.0+ KB
    
    # we have an accurate memory assessment (but can be expensive to compute this)
    In [7]: df.info(memory_usage="deep")
    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 1000 entries, 0 to 999
    Data columns (total 2 columns):
     #   Column  Non-Null Count  Dtype   
    ---  ------  --------------  -----   
     0   A       1000 non-null   object  
     1   B       1000 non-null   category
    dtypes: category(1), object(1)
    memory usage: 59.9 KB
    
  • Index 现在有一个 fillna 方法 (GH 10089)

    In [8]: pd.Index([1, np.nan, 3]).fillna(2)
    Out[8]: Index([1.0, 2.0, 3.0], dtype='float64')
    
  • 如果类别是该类型,类型为 category 的 Series 现在可以使用 .str.<...>.dt.<...> 访问器方法/属性 (GH 10661)

    In [9]: s = pd.Series(list("aabb")).astype("category")
    
    In [10]: s
    Out[10]: 
    0    a
    1    a
    2    b
    3    b
    Length: 4, dtype: category
    Categories (2, object): ['a', 'b']
    
    In [11]: s.str.contains("a")
    Out[11]: 
    0     True
    1     True
    2    False
    3    False
    Length: 4, dtype: bool
    
    In [12]: date = pd.Series(pd.date_range("1/1/2015", periods=5)).astype("category")
    
    In [13]: date
    Out[13]: 
    0   2015-01-01
    1   2015-01-02
    2   2015-01-03
    3   2015-01-04
    4   2015-01-05
    Length: 5, dtype: category
    Categories (5, datetime64[ns]): [2015-01-01, 2015-01-02, 2015-01-03, 2015-01-04, 2015-01-05]
    
    In [14]: date.dt.day
    Out[14]: 
    0    1
    1    2
    2    3
    3    4
    4    5
    Length: 5, dtype: int32
    
  • pivot_table 现在有一个 margins_name 参数,因此您可以使用除默认值“All”之外的其他名称 (GH 3335)

  • 实现了使用固定 HDF5 存储导出 datetime64[ns, tz] dtypes 的功能 (GH 11411)

  • 漂亮打印集合(例如 DataFrame 单元格中的集合)现在使用集合字面量语法 ({x, y}),而不是旧版 Python 语法 (set([x, y])) (GH 11215)

  • 改进了 pandas.io.gbq.to_gbq() 在流式插入失败 (GH 11285) 以及 DataFrame 与目标表的模式不匹配 (GH 11359) 时的错误消息。

API 变化#

  • 对于不支持的索引类型,在 Index.shift 中抛出 NotImplementedError (GH 8038)

  • datetime64timedelta64 dtyped Series 进行 minmax 归约现在会得到 NaT,而不是 nan (GH 11245)。

  • 使用 null 键进行索引将抛出 TypeError,而不是 ValueError (GH 11356)

  • Series.ptp 现在默认会忽略缺失值 (GH 11163)

已弃用#

  • 实现 google-analytics 支持的 pandas.io.ga 模块已被弃用,并将在未来版本中移除 (GH 11308)

  • .to_csv() 中的 engine 关键字已被弃用,并将在未来版本中移除 (GH 11274)

性能改进#

  • 在对索引进行排序之前检查单调性 (GH 11080)

  • Series.dropna 在其 dtype 不能包含 NaN 时的性能改进 (GH 11159)

  • 在大多数日期时间字段操作(例如 DatetimeIndex.year, Series.dt.year)、规范化以及与 PeriodDatetimeIndex.to_periodPeriodIndex.to_timestamp 之间的转换中释放 GIL (GH 11263)

  • 在一些滚动算法中释放 GIL:rolling_median, rolling_mean, rolling_max, rolling_min, rolling_var, rolling_kurt, rolling_skew (GH 11450)

  • read_csv, read_table 读取和解析文本文件时释放 GIL (GH 11272)

  • 改进了 rolling_median 的性能 (GH 11450)

  • 改进了 to_excel 的性能 (GH 11352)

  • 修复了 Categorical 类别的 repr 中的性能错误,之前在显示前会渲染字符串 (GH 11305)

  • 改进了 Categorical.remove_unused_categories 的性能 (GH 11643)。

  • 改进了没有数据和 DatetimeIndexSeries 构造函数的性能 (GH 11433)

  • 改进了带有 groupby 的 shift, cumprodcumsum 的性能 (GH 4095)

错误修复#

  • SparseArray.__iter__() 在 Python 3.5 中现在不会导致 PendingDeprecationWarning (GH 11622)

  • 修复了 0.16.2 版本中针对长浮点数/nan 输出格式化的回归错误,该问题在 (GH 11302) 中恢复。

  • Series.sort_index() 现在正确处理 inplace 选项 (GH 11402)

  • PyPi 构建中分发不正确的 .c 文件,当读取浮点数 csv 并传递 na_values=<a scalar> 时会显示异常 (GH 11374)

  • .to_latex() 输出在索引有名称时出现错误 (GH 10660)

  • HDFStore.append 在字符串编码长度超出最大未编码长度时的错误 (GH 11234)

  • 合并 datetime64[ns, tz] dtypes 时的错误 (GH 11405)

  • HDFStore.select 在 where 子句中与 numpy 标量进行比较时的错误 (GH 11283)

  • 使用 DataFrame.ix 和 MultiIndex 索引器时的错误 (GH 11372)

  • date_range 在端点模糊时的错误 (GH 11626)

  • 阻止向访问器 .str, .dt.cat 添加新属性。获取此类值是不可能的,因此在设置时报错 (GH 10673)

  • 使用模糊时间和 .dt 访问器进行时区转换时的错误 (GH 11295)

  • 使用模糊时间索引进行输出格式化时的错误 (GH 11619)

  • 比较 Series 与类列表对象时的错误 (GH 11339)

  • DataFrame.replace 在使用 datetime64[ns, tz] 和不兼容的 to_replace 参数时的错误 (GH 11326, GH 11153)

  • isnullnumpy.array 中的 numpy.datetime64('NaT') 未被判断为 null 的错误 (GH 11206)

  • 使用混合整数索引进行类列表索引时的错误 (GH 11320)

  • pivot_tablemargins=True 且索引为 Categorical dtype 时的错误 (GH 10993)

  • DataFrame.plot 不能使用十六进制字符串颜色表示的错误 (GH 10299)

  • 修复了 0.16.2 版本中 DataFrame.drop_duplicates 的回归错误,该错误导致整数值产生不正确的结果 (GH 11376)

  • pd.eval 在列表中包含一元操作符时出错的错误 (GH 11235)

  • squeeze() 在零长度数组时的错误 (GH 11230, GH 8999)

  • describe() 在处理分层索引时丢失列名的错误 (GH 11517)

  • DataFrame.pct_change() 未在 .fillna 方法上传播 axis 关键字参数的错误 (GH 11150)

  • .to_csv()columns 参数中混用整数和字符串列名时的错误 (GH 11637)

  • 使用 range 进行索引时的错误 (GH 11652)

  • 在设置列时推理 numpy 标量和保留 dtype 的错误 (GH 11638)

  • to_sql 使用 unicode 列名时导致 UnicodeEncodeError 的错误 (GH 11431)。

  • 修复了 plot 中设置 xticks 的回归错误 (GH 11529)。

  • holiday.dates 中无法将遵守规则应用于假期以及文档增强的错误 (GH 11477, GH 11533)

  • 修复了当使用普通 Axes 实例而非 SubplotAxes 时的绘图问题 (GH 11520, GH 11556)。

  • DataFrame.to_latex()header=False 时生成额外规则的错误 (GH 7124)

  • df.groupby(...).apply(func) 在 func 返回包含新日期时间类型列的 Series 时的错误 (GH 11324)

  • pandas.json 在加载文件过大时的错误 (GH 11344)

  • to_excel 处理重复列时的错误 (GH 11007, GH 10982, GH 10970)

  • 修复了阻止构造 dtype 为 datetime64[ns, tz] 的空 series 的错误 (GH 11245)。

  • read_excel 处理包含整数的 MultiIndex 时的错误 (GH 11317)

  • to_excel 在与 openpyxl 2.2+ 合并时的错误 (GH 11408)

  • DataFrame.to_dict() 在数据中仅存在 datetime 时生成 np.datetime64 对象而不是 Timestamp 的错误 (GH 11327)

  • DataFrame.corr() 在计算同时包含布尔类型和非布尔类型列的 DataFrame 的 Kendall 相关性时抛出异常的错误 (GH 11560)

  • 在 FreeBSD 10+ (使用 clang) 上由 C inline 函数导致的链接时错误 (GH 10510)

  • DataFrame.to_csv 在传递用于格式化 MultiIndexes 的参数(包括 date_format)时的错误 (GH 7791)

  • DataFrame.join()how='right' 时产生 TypeError 的错误 (GH 11519)

  • Series.quantile 使用空列表结果时,Index 的 dtype 为 object 的错误 (GH 11588)

  • pd.merge 在合并结果为空时产生空的 Int64Index 而不是 Index(dtype=object) 的错误 (GH 11588)

  • Categorical.remove_unused_categories 在包含 NaN 值时的错误 (GH 11599)

  • Bug in DataFrame.to_sparse() loses column names for MultiIndexes (GH 11600)

  • Bug in DataFrame.round() with non-unique column index producing a Fatal Python error (GH 11611)

  • Bug in DataFrame.round() with decimals being a non-unique indexed Series producing extra columns (GH 11618)

贡献者#

共有 63 人为本次发布贡献了补丁。名字旁带有 “+” 的人是首次贡献补丁。

  • Aleksandr Drozd +

  • Alex Chase +

  • Anthonios Partheniou

  • BrenBarn +

  • Brian J. McGuirk +

  • Chris

  • Christian Berendt +

  • Christian Perez +

  • Cody Piersall +

  • Data & Code Expert Experimenting with Code on Data

  • DrIrv +

  • Evan Wright

  • Guillaume Gay

  • Hamed Saljooghinejad +

  • Iblis Lin +

  • Jake VanderPlas

  • Jan Schulz

  • Jean-Mathieu Deschenes +

  • Jeff Reback

  • Jimmy Callin +

  • Joris Van den Bossche

  • K.-Michael Aye

  • Ka Wo Chen

  • Loïc Séguin-C +

  • Luo Yicheng +

  • Magnus Jöud +

  • Manuel Leonhardt +

  • Matthew Gilbert

  • Maximilian Roos

  • Michael +

  • Nicholas Stahl +

  • Nicolas Bonnotte +

  • Pastafarianist +

  • Petra Chong +

  • Phil Schaf +

  • Philipp A +

  • Rob deCarvalho +

  • Roman Khomenko +

  • Rémy Léone +

  • Sebastian Bank +

  • Sinhrks

  • Stephan Hoyer

  • Thierry Moisan

  • Tom Augspurger

  • Tux1 +

  • Varun +

  • Wieland Hoffmann +

  • Winterflower

  • Yoav Ram +

  • Younggun Kim

  • Zeke +

  • ajcr

  • azuranski +

  • behzad nouri

  • cel4

  • emilydolson +

  • hironow +

  • lexual

  • llllllllll +

  • rockg

  • silentquasar +

  • sinhrks

  • taeold +