版本 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)
    

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

    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 数据类型,以便将此类值绘制为日期时间。这在导入 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]
    
  • 使用基于层大小利用内存的 dtype 来表示 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() 中添加了对三字符 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 的日期时间与标量日期时间进行比较时引发错误的 bug (GH 8687)

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

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

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

  • 如果第一个参数是 numpy 数组标量(例如 np.int64),则 Categorical 反射比较操作符引发错误的 bug (GH 8658)

  • 使用类列表对象对 Panel 进行索引时的错误 (GH 8710)

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

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

  • 使用空列表对 MultiIndex 级别进行切片时的错误 (GH 8737)

  • 在 Float/Index Index 与 numpy 数组进行数值索引的加/减操作中的错误 (GH 8608)

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

  • 在 setitem 上进行 ix/loc 块拆分时的错误(表现为整数类 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