我有一个以DatetimeIndex为索引的Pandas数据框:
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 53732 entries, 1993-01-07 12:23:58 to 2012-12-02 20:06:23
Data columns:
Date(dd-mm-yy)_Time(hh-mm-ss) 53732 non-null values
Julian_Day 53732 non-null values
AOT_870 53732 non-null values
440-870Angstrom 53732 non-null values
440-675Angstrom 53732 non-null values
500-870Angstrom 53732 non-null values
Last_Processing_Date(dd/mm/yyyy) 53732 non-null values
Solar_Zenith_Angle 53732 non-null values
time 53732 non-null values
dtypes: datetime64[ns](2), float64(6), object(1)
我希望找到最接近特定时间的行:
image_time = dateutil.parser.parse('2009-07-28 13:39:02')
我会翻译中文。以下是需要翻译的内容:
并找出它有多接近。到目前为止,我尝试了各种基于减去我想要的时间并找到最小绝对值的想法,但似乎都不太行。
例如:
aeronet.index - image_time
出现了一个错误,我认为是由于日期时间索引上的+/-导致的偏移,因此我尝试将索引放入另一列中,然后在该列上进行操作:
aeronet['time'] = aeronet.index
aeronet.time - image_time
这似乎可行,但为了达到我的目的,我需要获取绝对时间差,而不是相对差异。但是,仅运行
abs
或np.abs
会导致错误:abs(aeronet.time - image_time)
C:\Python27\lib\site-packages\pandas\core\series.pyc in __repr__(self)
1061 Yields Bytestring in Py2, Unicode String in py3.
1062 """
-> 1063 return str(self)
1064
1065 def _tidy_repr(self, max_vals=20):
C:\Python27\lib\site-packages\pandas\core\series.pyc in __str__(self)
1021 if py3compat.PY3:
1022 return self.__unicode__()
-> 1023 return self.__bytes__()
1024
1025 def __bytes__(self):
C:\Python27\lib\site-packages\pandas\core\series.pyc in __bytes__(self)
1031 """
1032 encoding = com.get_option("display.encoding")
-> 1033 return self.__unicode__().encode(encoding, 'replace')
1034
1035 def __unicode__(self):
C:\Python27\lib\site-packages\pandas\core\series.pyc in __unicode__(self)
1044 else get_option("display.max_rows"))
1045 if len(self.index) > (max_rows or 1000):
-> 1046 result = self._tidy_repr(min(30, max_rows - 4))
1047 elif len(self.index) > 0:
1048 result = self._get_repr(print_header=True,
C:\Python27\lib\site-packages\pandas\core\series.pyc in _tidy_repr(self, max_vals)
1069 """
1070 num = max_vals // 2
-> 1071 head = self[:num]._get_repr(print_header=True, length=False,
1072 name=False)
1073 tail = self[-(max_vals - num):]._get_repr(print_header=False,
AttributeError: 'numpy.ndarray' object has no attribute '_get_repr'
我这样做是正确的吗?如果是,我该如何让abs
工作,以便我可以选择最小的绝对时间差,从而得到最接近的时间。如果不是,那么使用Pandas时间序列的最佳方法是什么?