版本 0.15.1 (2014 年 11 月 9 日)#

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

API 更改#

  • s.dt.hour 和其他 .dt 访问器现在将对缺失值返回 np.nan(而不是之前的 -1),(GH 8689)

    In [1]: s = pd.Series(pd.date_range("20130101", periods=5, freq="D"))
    
    In [2]: s.iloc[2] = np.nan
    
    In [3]: s
    Out[3]: 
    0   2013-01-01
    1   2013-01-02
    2          NaT
    3   2013-01-04
    4   2013-01-05
    Length: 5, dtype: datetime64[ns]
    

    之前的行为

    In [6]: s.dt.hour
    Out[6]:
    0    0
    1    0
    2   -1
    3    0
    4    0
    dtype: int64
    

    当前的行为

    In [4]: s.dt.hour
    Out[4]: 
    0    0.0
    1    0.0
    2    NaN
    3    0.0
    4    0.0
    Length: 5, dtype: float64
    
  • 使用 as_index=Falsegroupby 不会向结果添加错误的额外列 (GH 8582)

    In [5]: np.random.seed(2718281)
    
    In [6]: df = pd.DataFrame(np.random.randint(0, 100, (10, 2)), columns=["jim", "joe"])
    
    In [7]: df.head()
    Out[7]: 
       jim  joe
    0   61   81
    1   96   49
    2   55   65
    3   72   51
    4   77   12
    
    [5 rows x 2 columns]
    
    In [8]: ts = pd.Series(5 * np.random.randint(0, 3, 10))
    

    之前的行为

    In [4]: df.groupby(ts, as_index=False).max()
    Out[4]:
       NaN  jim  joe
    0    0   72   83
    1    5   77   84
    2   10   96   65
    

    当前的行为

    In [4]: df.groupby(ts, as_index=False).max()
    Out[4]:
       jim  joe
    0   72   83
    1   77   84
    2   96   65
    
  • 如果列名与分组器名称冲突,groupby 不会错误地排除列 (GH 8112)

    In [9]: df = pd.DataFrame({"jim": range(5), "joe": range(5, 10)})
    
    In [10]: df
    Out[10]: 
       jim  joe
    0    0    5
    1    1    6
    2    2    7
    3    3    8
    4    4    9
    
    [5 rows x 2 columns]
    
    In [11]: gr = df.groupby(df["jim"] < 2)
    

    之前的行为(从输出中排除第 1 列)

    In [4]: gr.apply(sum)
    Out[4]:
           joe
    jim
    False   24
    True    11
    

    当前的行为

    In [12]: gr.apply(sum)
    Out[12]: 
           jim  joe
    jim            
    False    9   24
    True     1   11
    
    [2 rows x 2 columns]
    
  • 支持使用单调递减索引进行切片,即使 startstop 在索引中未找到 (GH 7860)

    In [13]: s = pd.Series(["a", "b", "c", "d"], [4, 3, 2, 1])
    
    In [14]: s
    Out[14]: 
    4    a
    3    b
    2    c
    1    d
    Length: 4, dtype: object
    

    之前的行为

    In [8]: s.loc[3.5:1.5]
    KeyError: 3.5
    

    当前的行为

    In [15]: s.loc[3.5:1.5]
    Out[15]: 
    3    b
    2    c
    Length: 2, dtype: object
    
  • io.data.Options 已修复以适应 Yahoo 期权页面格式的更改 (GH 8612), (GH 8741)

    注意

    由于 Yahoo 期权页面布局的更改,当指定到期日期时,Options 方法现在返回单个到期日期的数据。之前,方法返回所选月份的所有数据。

    monthyear 参数已取消弃用,可用于获取给定月份的所有期权数据。

    如果指定的到期日期无效,则返回指定日期后下一个到期日期的数据。

    期权数据框现在在实例上保存为 callsYYMMDDputsYYMMDD。之前它们保存为 callsMMYYputsMMYY。下一个到期日期的数据保存为 callsputs

    新功能

    • 现在 expiry 参数可以是一个单个日期或一个包含日期的列表状对象。

    • 新增了一个属性 expiry_dates,它返回所有可用的到期日期。

    当前的行为

    In [17]: from pandas.io.data import Options
    
    In [18]: aapl = Options('aapl', 'yahoo')
    
    In [19]: aapl.get_call_data().iloc[0:5, 0:1]
    Out[19]:
                                                 Last
    Strike Expiry     Type Symbol
    80     2014-11-14 call AAPL141114C00080000  29.05
    84     2014-11-14 call AAPL141114C00084000  24.80
    85     2014-11-14 call AAPL141114C00085000  24.05
    86     2014-11-14 call AAPL141114C00086000  22.76
    87     2014-11-14 call AAPL141114C00087000  21.74
    
    In [20]: aapl.expiry_dates
    Out[20]:
    [datetime.date(2014, 11, 14),
     datetime.date(2014, 11, 22),
     datetime.date(2014, 11, 28),
     datetime.date(2014, 12, 5),
     datetime.date(2014, 12, 12),
     datetime.date(2014, 12, 20),
     datetime.date(2015, 1, 17),
     datetime.date(2015, 2, 20),
     datetime.date(2015, 4, 17),
     datetime.date(2015, 7, 17),
     datetime.date(2016, 1, 15),
     datetime.date(2017, 1, 20)]
    
    In [21]: aapl.get_near_stock_price(expiry=aapl.expiry_dates[0:3]).iloc[0:5, 0:1]
    Out[21]:
                                                Last
    Strike Expiry     Type Symbol
    109    2014-11-22 call AAPL141122C00109000  1.48
           2014-11-28 call AAPL141128C00109000  1.79
    110    2014-11-14 call AAPL141114C00110000  0.55
           2014-11-22 call AAPL141122C00110000  1.02
           2014-11-28 call AAPL141128C00110000  1.32
    
  • pandas 现在还在 matplotlib 的单位注册表中注册了 datetime64 数据类型,以便将这些值绘制为 datetime。这在导入 pandas 后即被激活。在之前的版本中,绘制 datetime64 值数组会绘制为整数值。要保留之前的行为,您可以执行 del matplotlib.units.registry[np.datetime64] (GH 8614)。

增强功能#

  • concat 允许将更多种类的 pandas 对象迭代器作为第一个参数传入 (GH 8645)

    In [16]: from collections import deque
    
    In [17]: df1 = pd.DataFrame([1, 2, 3])
    
    In [18]: df2 = pd.DataFrame([4, 5, 6])
    

    之前的行为

    In [7]: pd.concat(deque((df1, df2)))
    TypeError: first argument must be a list-like of pandas objects, you passed an object of type "deque"
    

    当前的行为

    In [19]: pd.concat(deque((df1, df2)))
    Out[19]: 
       0
    0  1
    1  2
    2  3
    0  4
    1  5
    2  6
    
    [6 rows x 1 columns]
    
  • 使用基于层级大小利用内存的数据类型表示 MultiIndex 标签。在之前的版本中,每个层级中每个元素的内存使用是固定的 8 字节。此外,在之前的版本中,报告的内存使用是不正确的,因为它没有显示底层数据数组占用的内存使用情况。(GH 8456)

    In [20]: dfi = pd.DataFrame(
       ....:     1, index=pd.MultiIndex.from_product([["a"], range(1000)]), columns=["A"]
       ....: )
       ....: 
    

    之前的行为

    # this was underreported in prior versions
    In [1]: dfi.memory_usage(index=True)
    Out[1]:
    Index    8000 # took about 24008 bytes in < 0.15.1
    A        8000
    dtype: int64
    

    当前的行为

    In [21]: dfi.memory_usage(index=True)
    Out[21]: 
    Index    44212
    A         8000
    Length: 2, dtype: int64
    
  • 添加了 Index 属性 is_monotonic_increasingis_monotonic_decreasing (GH 8680)。

  • 添加了导入 Stata 文件时选择列的选项 (GH 7935)

  • DataFrame.info() 中,如果内存使用是一个下限,则通过添加 + 来限定其含义 (GH 8578)

  • 在某些聚合情况下,如果未处理 numeric_only 等参数,则会引发错误 (GH 8592)。

  • io.wb.download() 中添加了对 3 字符 ISO 和非标准国家代码的支持 (GH 8482)

  • 世界银行数据请求现在将根据 errors 参数以及硬编码的国家代码列表和世界银行的 JSON 响应发出警告/引发异常。在之前的版本中,错误消息没有查看世界银行的 JSON 响应。导致问题的输入在请求之前被简单地丢弃。问题在于,在硬编码方法中,许多有效的国家代码被截断了。现在所有国家都将正常工作,但由于某些边缘情况会破坏整个响应,一些无效的国家代码将引发异常。(GH 8482)

  • 添加了 Series.str.split() 返回 DataFrame 而不是 Series 的选项 (GH 8428)

  • 添加了 df.info(null_counts=None|True|False) 的选项,用于覆盖默认显示选项并强制显示空值计数 (GH 8701)

错误修复#

  • 在反序列化 CustomBusinessDay 对象时出现错误 (GH 8591)

  • 在将 Categorical 强制转换为记录数组时出现错误,例如 df.to_records() (GH 8626)

  • 使用 Series.to_frame() 创建 Categorical 不正确时出现错误 (GH 8626)

  • 在对已传递的 pd.CategoricalCategorical 进行 astype 强制转换时出现错误(现在会正确地引发 TypeError),(GH 8626)

  • 使用 Seriesretbins=True 时,cut/qcut 中的错误 (GH 8589)

  • 使用 to_sql 将 Categorical 列写入 SQL 数据库时出现的错误 (GH 8624)。

  • 在将日期时间的 Categorical 与标量日期时间进行比较时引发异常的错误 (GH 8687)

  • 使用 .ilocCategorical 中选择时出现的错误 (GH 8623)

  • 使用 Categorical 进行 groupby-transform 时出现的错误 (GH 8623)

  • 使用 Categorical 进行 duplicated/drop_duplicates 时出现的错误 (GH 8623)

  • 如果第一个参数是 numpy 数组标量(例如 np.int64),Categorical 的反射比较运算符会引发异常的错误 (GH 8658)

  • 使用 list-like 对 Panel 进行索引时出现的错误 (GH 8710)

  • options.mode.use_inf_as_null 为 True 时,DataFrame.dtypes 的兼容性问题 (GH 8722)

  • read_csv 中的错误,dialect 参数不接受字符串 (GH 8703)

  • 使用空列表切片 MultiIndex 层级时出现的错误 (GH 8737)

  • 使用 numpy 数组对 Float/Index Index 进行加/减等数值索引操作时出现的错误 (GH 8608)

  • 使用空索引器进行 setitem 以及不必要的 dtype 强制转换时出现的错误 (GH 8669)

  • 在使用 ix/loc 进行 setitem 时块分割的错误(在使用整数类 dtype 时出现,例如 datetime64)(GH 8607)

  • 对于非唯一但单调的索引,使用索引中未找到的整数进行基于标签的索引时出现的错误 (GH 8680)。

  • 在 numpy 1.7 上使用 np.nan 对 Float64Index 进行索引时出现的错误 (GH 8980)。

  • 修复 MultiIndexshape 属性 (GH 8609)

  • GroupBy 中,分组器和列之间的名称冲突会导致 groupby 操作中断的错误 (GH 7115, GH 8112)

  • 修复了一个错误,即绘制列 y 并指定标签会改变原始 DataFrame 的索引名称 (GH 8494)

  • 修复了使用 matplotlib 直接绘制 DatetimeIndex 的回归错误 (GH 8614)。

  • date_range 中的错误,其中部分指定的日期会包含当前日期 (GH 6961)

  • 使用混合 dtype 的 Panel4d 通过索引器设置为标量值时失败的错误 (GH 8702)

  • 如果传递的符号中有一个无效,DataReader 会失败的错误。现在对有效的符号返回数据,对无效的返回 np.nan (GH 8494)

  • get_quote_yahoo 不允许非浮点返回值的问题 (GH 5229)。

贡献者#

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

  • Aaron Staple +

  • Andrew Rosenfeld

  • Anton I. Sipos

  • Artemy Kolchinsky

  • Bill Letson +

  • Dave Hughes +

  • David Stephens

  • Guillaume Horel +

  • Jeff Reback

  • Joris Van den Bossche

  • Kevin Sheppard

  • Nick Stahl +

  • Sanghee Kim +

  • Stephan Hoyer

  • Tom Augspurger

  • TomAugspurger

  • WANG Aiyong +

  • behzad nouri

  • immerrr

  • jnmclarty

  • jreback

  • pallav-fdsi +

  • unutbu