Pandas系列.to_sparse()引发:TypeError("无法连接非NDFrame对象")

时间:2016-05-11 10:38:43

标签: python pandas sparse-matrix

我有一个Pandas系列对象(LINK TO DATA EXAMPLE):

type(ts)
Out[54]: pandas.core.series.Series

ts.isnull().sum()
Out[16]: 2744

ts.size
Out[17]: 4096

ts.dtype
Out[19]: dtype('float64')

当我尝试使用to_sparse

将系列转换为Sparse data structure时,我收到此错误
ts.to_sparse()
Traceback (most recent call last):

  File "/home/david/anaconda3/lib/python3.5/site-packages/IPython/core/formatters.py", line 699, in __call__
    printer.pretty(obj)

  File "/home/david/anaconda3/lib/python3.5/site-packages/IPython/lib/pretty.py", line 383, in pretty
    return _default_pprint(obj, self, cycle)

  File "/home/david/anaconda3/lib/python3.5/site-packages/IPython/lib/pretty.py", line 503, in _default_pprint
    _repr_pprint(obj, p, cycle)

  File "/home/david/anaconda3/lib/python3.5/site-packages/IPython/lib/pretty.py", line 694, in _repr_pprint
    output = repr(obj)

  File "/home/david/anaconda3/lib/python3.5/site-packages/pandas/core/base.py", line 63, in __repr__
    return str(self)

  File "/home/david/anaconda3/lib/python3.5/site-packages/pandas/core/base.py", line 42, in __str__
    return self.__unicode__()

  File "/home/david/anaconda3/lib/python3.5/site-packages/pandas/sparse/series.py", line 287, in __unicode__
    series_rep = Series.__unicode__(self)

  File "/home/david/anaconda3/lib/python3.5/site-packages/pandas/core/series.py", line 959, in __unicode__
    max_rows=max_rows)

  File "/home/david/anaconda3/lib/python3.5/site-packages/pandas/core/series.py", line 1000, in to_string
    dtype=dtype, name=name, max_rows=max_rows)

  File "/home/david/anaconda3/lib/python3.5/site-packages/pandas/core/series.py", line 1027, in _get_repr
    max_rows=max_rows)

  File "/home/david/anaconda3/lib/python3.5/site-packages/pandas/core/format.py", line 144, in __init__
    self._chk_truncate()

  File "/home/david/anaconda3/lib/python3.5/site-packages/pandas/core/format.py", line 158, in _chk_truncate
    series.iloc[-row_num:]))

  File "/home/david/anaconda3/lib/python3.5/site-packages/pandas/tools/merge.py", line 834, in concat
    copy=copy)

  File "/home/david/anaconda3/lib/python3.5/site-packages/pandas/tools/merge.py", line 890, in __init__
    raise TypeError("cannot concatenate a non-NDFrame object")

TypeError: cannot concatenate a non-NDFrame object

我真的不明白为什么会出现这种错误,我想知道发生了什么。

1 个答案:

答案 0 :(得分:0)

it works fine for me (pandas 0.18.1):

In [183]: ts.to_sparse()
Out[183]:
0       0.00200
1       0.00116
2       0.00138
3       0.00141
4       0.00138
5       0.00121
6       0.00134
7       0.00148
8       0.00134
9       0.00119
10      0.00136
11      0.00136
12      0.00114
13      0.00122
14      0.00146
15      0.00222
16      0.00132
17      0.00060
18      0.00058
19      0.00049
20      0.00067
21      0.00064
22      0.00066
23      0.00051
24      0.00065
25      0.00055
26      0.00077
27      0.00066
28      0.00062
29      0.00066
         ...
4066    0.00048
4067    0.00050
4068    0.00074
4069    0.00058
4070    0.00072
4071    0.00068
4072    0.00060
4073    0.00067
4074    0.00065
4075    0.00066
4076    0.00061
4077    0.00072
4078    0.00070
4079    0.00121
4080    0.00206
4081    0.00138
4082    0.00131
4083    0.00124
4084    0.00116
4085    0.00127
4086    0.00131
4087    0.00127
4088    0.00134
4089    0.00147
4090    0.00148
4091    0.00143
4092    0.00121
4093    0.00140
4094    0.00143
4095    0.00184
Name: ts, dtype: float64
BlockIndex
Block locations: array([   0,  287,  303,  319,  335,  351,  367,  383,  399,  415,  431,
        447,  463,  479,  495,  543,  559,  575,  591,  607,  623,  639,
        655,  671,  687,  703,  719,  735,  751,  799,  815,  831,  847,
        863,  879,  895,  911,  927,  943,  959,  975,  991, 1007, 1055,
       1071, 1087, 1103, 1119, 1135, 1151, 1167, 1183, 1199, 1215, 1231,
       1247, 1263, 1311, 1327, 1343, 1359, 1375, 1391, 1407, 1423, 1439,
       1455, 1471, 1487, 1503, 1519, 1567, 1583, 1599, 1615, 1631, 1647,
       1663, 1679, 1695, 1711, 1727, 1743, 1759, 1775, 1823, 1839, 1855,
       1871, 1887, 1903, 1919, 1935, 1951, 1967, 1983, 1999, 2015, 2031,
       2079, 2095, 2111, 2127, 2143, 2159, 2175, 2191, 2207, 2223, 2239,
       2255, 2271, 2287, 2335, 2351, 2367, 2383, 2399, 2415, 2431, 2447,
       2463, 2479, 2495, 2511, 2527, 2543, 2591, 2607, 2623, 2639, 2655,
       2671, 2687, 2703, 2719, 2735, 2751, 2767, 2783, 2799, 2847, 2863,
       2879, 2895, 2911, 2927, 2943, 2959, 2975, 2991, 3007, 3023, 3039,
       3055, 3103, 3119, 3135, 3151, 3167, 3183, 3199, 3215, 3231, 3247,
       3263, 3279, 3295, 3311, 3359, 3375, 3391, 3407, 3423, 3439, 3455,
       3471, 3487, 3503, 3519, 3535, 3551, 3567, 3615, 3631, 3647, 3663,
       3679, 3695, 3711, 3727, 3743, 3759, 3775, 3791, 3807, 3823])
Block lengths: array([273,   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,
         2,  34,   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,
         2,   2,  34,   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,
         2,   2,   2,  34,   2,   2,   2,   2,   2,   2,   2,   2,   2,
         2,   2,   2,   2,  34,   2,   2,   2,   2,   2,   2,   2,   2,
         2,   2,   2,   2,   2,  34,   2,   2,   2,   2,   2,   2,   2,
         2,   2,   2,   2,   2,   2,  34,   2,   2,   2,   2,   2,   2,
         2,   2,   2,   2,   2,   2,   2,  34,   2,   2,   2,   2,   2,
         2,   2,   2,   2,   2,   2,   2,   2,  34,   2,   2,   2,   2,
         2,   2,   2,   2,   2,   2,   2,   2,   2,  34,   2,   2,   2,
         2,   2,   2,   2,   2,   2,   2,   2,   2,   2,  34,   2,   2,
         2,   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,  34,   2,
         2,   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,  34,
         2,   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,
        34,   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,   2,
         2, 273])

In [184]: ts.dtype
Out[184]: dtype('float64')