我从下面的评论中了解到,这是一种需要调用的方法,但是我不了解按日期时间进行过滤的整个概念,并且互联网上的任何问题都没有答案。有人可以请我逐步介绍按年份进行过滤需要做什么。
此代码用于过滤某些日期范围之间的行。在打印数据框时,我只希望打印出这些日期之间的行,但是遇到错误(在我的代码下方列出)。我不确定如何转换数据以匹配正确的数据类型。关于如何运行代码以仅打印出该范围内的行的任何想法?
import pandas as pd
import datetime
df = pd.read_csv('otd.csv', encoding='latin-1')
dd = df['Delivery Date']
startdate = pd.to_datetime("12/31/2014").date
enddate = pd.to_datetime("01/05/2015").date
print(df.loc[startdate:enddate])
错误:
Traceback (most recent call last):
File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 958, in pandas._libs.hashtable.Int64HashTable.get_item
TypeError: an integer is required
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\khalha\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 3078, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 164, in pandas._libs.index.IndexEngine.get_loc
KeyError: <built-in method date of Timestamp object at 0x000000000BE91148>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 958, in pandas._libs.hashtable.Int64HashTable.get_item
TypeError: an integer is required
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\khalha\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 4238, in get_slice_bound
slc = self._get_loc_only_exact_matches(label)
File "C:\Users\khalha\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 4207, in _get_loc_only_exact_matches
return self.get_loc(key)
File "C:\Users\khalha\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 164, in pandas._libs.index.IndexEngine.get_loc
KeyError: <built-in method date of Timestamp object at 0x000000000BE91148>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\khalha\eclipse-workspace\Test3\Project3\P3.py", line 10, in <module>
print(df.loc[startdate:enddate])
File "C:\Users\khalha\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexing.py", line 1478, in __getitem__
return self._getitem_axis(maybe_callable, axis=axis)
File "C:\Users\khalha\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexing.py", line 1866, in _getitem_axis
return self._get_slice_axis(key, axis=axis)
File "C:\Users\khalha\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexing.py", line 1511, in _get_slice_axis
slice_obj.step, kind=self.name)
File "C:\Users\khalha\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 4107, in slice_indexer
kind=kind)
File "C:\Users\khalha\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 4308, in slice_locs
start_slice = self.get_slice_bound(start, 'left', kind)
File "C:\Users\khalha\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 4241, in get_slice_bound
return self._searchsorted_monotonic(label, side)
File "C:\Users\khalha\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 4191, in _searchsorted_monotonic
return self.searchsorted(label, side=side)
File "C:\Users\khalha\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\util\_decorators.py", line 178, in wrapper
return func(*args, **kwargs)
File "C:\Users\khalha\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\base.py", line 1230, in searchsorted
return self.values.searchsorted(value, side=side, sorter=sorter)
TypeError: '<' not supported between instances of 'builtin_function_or_method' and 'builtin_function_or_method'
这是一个示例csv文件: