qq_30089875
qq_30089875
采纳率60%
2019-01-24 18:25 阅读 1.9k

【python】关于Pandas DataFrame 的一些奇怪的问题

具体代码如下:

def reshape_data(time_today, data_today):
    #构建时间序列数组
    len_data = len(data_today)
    time_try = [parse(time_today)]*len_data
    #插入时间轴并定为最外轴
    data_today.insert(0, 'time', time_try)
    data_today.set_index(['time',data_today.index], inplace = True)
    return data_today

输入为一个字符串time_today和一个DataFrame data_today
然后在实际运行中提示出错,详细错误代码如下:

NotImplementedError    Traceback (most recent call last)
<ipython-input-14-d4c768794e17> in <module>
1 if __name__ == '__main__':
----> 2     main()
      3     rootdir

<ipython-input-8-0c303725fb93> in main()
     15     test_b = caculate_rate(a)
     16     print(test_b)
---> 17     test_b = reshape_data(time_a, test_b)
     18     save_csv(test_b, file_name_test, False)
     19     b = save_csv(test_a, file_name_test, True)

<ipython-input-13-1381f1d66cba> in reshape_data(time_today, data_today)
      9     data_today.index.names = ['name']
     10     data_today = data_today.reset_index()
---> 11     data_today.set_index(['time','name'], inplace = True)
     12 
     13     return data_today

G:\anaconda\lib\site-packages\pandas\core\frame.py in set_index(self, keys, drop, append, inplace, verify_integrity)
   3913             arrays.append(level)
   3914 
-> 3915         index = _ensure_index_from_sequences(arrays, names)
   3916 
   3917         if verify_integrity and not index.is_unique:

G:\anaconda\lib\site-packages\pandas\core\indexes\base.py in _ensure_index_from_sequences(sequences, names)
   4909         return Index(sequences[0], name=names)
   4910     else:
-> 4911         return MultiIndex.from_arrays(sequences, names=names)
   4912 
   4913 

G:\anaconda\lib\site-packages\pandas\core\indexes\multi.py in from_arrays(cls, arrays, sortorder, names)
   1272         from pandas.core.arrays.categorical import _factorize_from_iterables
   1273 
-> 1274         labels, levels = _factorize_from_iterables(arrays)
   1275         if names is None:
   1276             names = [getattr(arr, "name", None) for arr in arrays]

G:\anaconda\lib\site-packages\pandas\core\arrays\categorical.py in _factorize_from_iterables(iterables)
   2541         # For consistency, it should return a list of 2 lists.
   2542         return [[], []]
-> 2543     return map(list, lzip(*[_factorize_from_iterable(it) for it in iterables]))

G:\anaconda\lib\site-packages\pandas\core\arrays\categorical.py in <listcomp>(.0)
   2541         # For consistency, it should return a list of 2 lists.
   2542         return [[], []]
-> 2543     return map(list, lzip(*[_factorize_from_iterable(it) for it in iterables]))

G:\anaconda\lib\site-packages\pandas\core\arrays\categorical.py in _factorize_from_iterable(values)
   2513         codes = values.codes
   2514     else:
-> 2515         cat = Categorical(values, ordered=True)
   2516         categories = cat.categories
   2517         codes = cat.codes

G:\anaconda\lib\site-packages\pandas\core\arrays\categorical.py in __init__(self, values, categories, ordered, dtype, fastpath)
    355 
    356                 # FIXME
--> 357                 raise NotImplementedError("> 1 ndim Categorical are not "
    358                                           "supported at this time")
    359 

NotImplementedError: > 1 ndim Categorical are not supported at this time

这是什么神奇的事情啊?其他地方都没事,只有这里调用的时候出错,连语法都是一样的。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    u012111465 Watch_dou 2019-01-24 19:07

    不知道你的那个parse()函数是干啥的,去掉parse(),以下是没问题的

    >>>
    >>> time_today = '2019-01-24'
    >>> data_today = pd.DataFrame({'key':['a','b','c'],'data1':[1,2,3],'data2':[4,5,6]})
    >>> #构建时间序列数组
    ... len_data = len(data_today)
    >>> time_try = [time_today]*len_data
    >>> time_try
    ['2019-01-24', '2019-01-24', '2019-01-24']
    >>> #time_try = [parse(time_today)]*len_data
    ... #插入时间轴并定为最外轴
    ... data_today.insert(0, 'time', time_try)
    >>> data_today
             time key  data1  data2
    0  2019-01-24   a      1      4
    1  2019-01-24   b      2      5
    2  2019-01-24   c      3      6
    >>> data_today.set_index(['time',data_today.index], inplace = True)
    >>> data_today
                 key  data1  data2
    time
    2019-01-24 0   a      1      4
               1   b      2      5
               2   c      3      6
    >>>
    
    
    点赞 评论 复制链接分享

相关推荐