【问题标题】:Pandas.DataFrame.dropna does not work when index is datetime当索引为日期时间时,Pandas.DataFrame.dropna 不起作用
【发布时间】:2018-01-24 23:42:01
【问题描述】:

我有一个日期时间索引如下的数据框:

我想删除所有具有 NaN 值的行,所以我过去只是应用 Pandas.DataFrame.dropna 来删除所有行,但是当我将它用于具有索引的数据框时,会出现以下错误:

C:\Users\nandhos\Anaconda2\lib\site-packages\pandas\core\frame.pyc in dropna(self, axis, how, thresh, subset, inplace)
   3067                     raise TypeError('must specify how or thresh')
   3068 
-> 3069             result = self.take(mask.nonzero()[0], axis=axis, convert=False)
   3070 
   3071         if inplace:

C:\Users\nandhos\Anaconda2\lib\site-packages\pandas\core\generic.pyc in take(self, indices, axis, convert, is_copy, **kwargs)
   1926         new_data = self._data.take(indices,
   1927                                    axis=self._get_block_manager_axis(axis),
-> 1928                                    convert=True, verify=True)
   1929         result = self._constructor(new_data).__finalize__(self)
   1930 

C:\Users\nandhos\Anaconda2\lib\site-packages\pandas\core\internals.pyc in take(self, indexer, axis, verify, convert)
   4009         new_labels = self.axes[axis].take(indexer)
   4010         return self.reindex_indexer(new_axis=new_labels, indexer=indexer,
-> 4011                                     axis=axis, allow_dups=True)
   4012 
   4013     def merge(self, other, lsuffix='', rsuffix=''):

C:\Users\nandhos\Anaconda2\lib\site-packages\pandas\core\internals.pyc in reindex_indexer(self, new_axis, indexer, axis, fill_value, allow_dups, copy)
   3895             new_blocks = [blk.take_nd(indexer, axis=axis, fill_tuple=(
   3896                 fill_value if fill_value is not None else blk.fill_value,))
-> 3897                 for blk in self.blocks]
   3898 
   3899         new_axes = list(self.axes)

C:\Users\nandhos\Anaconda2\lib\site-packages\pandas\core\internals.pyc in take_nd(self, indexer, axis, new_mgr_locs, fill_tuple)
   1044             fill_value = fill_tuple[0]
   1045             new_values = algos.take_nd(values, indexer, axis=axis,
-> 1046                                        allow_fill=True, fill_value=fill_value)
   1047 
   1048         if new_mgr_locs is None:

C:\Users\nandhos\Anaconda2\lib\site-packages\pandas\core\algorithms.pyc in take_nd(arr, indexer, axis, out, fill_value, mask_info, allow_fill)
   1469     func = _get_take_nd_function(arr.ndim, arr.dtype, out.dtype, axis=axis,
   1470                                  mask_info=mask_info)
-> 1471     func(arr, indexer, out, fill_value)
   1472 
   1473     if flip_order:

pandas\_libs\algos_take_helper.pxi in pandas._libs.algos.take_2d_axis1_float32_float32 (pandas\_libs\algos.c:106602)()

ValueError: Big-endian buffer not supported on little-endian compiler

索引只有数字时不会出现该错误,如下所示:

【问题讨论】:

  • 对我来说似乎工作正常,你运行的是什么版本的熊猫?
  • 这没有任何意义,因为 df.dropna().head() 正在调用 dropna 函数并已经执行它,或者我错过了什么?

标签: python python-2.7 pandas datetime dataframe


【解决方案1】:

尝试使用 isfinite() 代替:

import numpy as np
df = df[np.isfinite(df['pwv'])]

【讨论】:

  • 我试过了,但我有同样的错误 ValueError: Big-endian buffer not supported on little-endian compiler
猜你喜欢
  • 2011-04-30
  • 1970-01-01
  • 2020-05-27
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 2013-05-07
相关资源
最近更新 更多