Из комментария ниже я понимаю, что этот метод необходимо вызывать, однако я не понимаю всей этой концепции фильтрации по дате и времени, и ни одна проблема в Интернете не дает ответа. Может кто-нибудь, пожалуйста, проведите меня через то, что нужно сделать, чтобы отфильтровать по годам.
Этот код используется для фильтрации строк между определенным диапазоном дат. Я хочу распечатать только те строки, которые лежат между этими датами, когда я печатаю свой фрейм данных, однако сталкиваюсь с ошибкой (перечисленной ниже моего кода). Я не уверен, как мне следует преобразовать мои данные, чтобы они соответствовали правильному типу данных. Любые идеи о том, как я могу выполнить код, чтобы распечатать только строки в этом диапазоне?
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-файла:
![enter image description here](https://i.stack.imgur.com/L38Z7.png)