python-熊猫:如何按日期选择

python-熊猫:如何按日期选择,python,pandas,Python,Pandas,在这种情况下,为什么我可以按月份进行选择,而不能按日期进行选择 dates = pd.date_range( start = "01/01/1931" , end = "01/02/1941" ) new_df_4 = new_df_3.reindex(dates) new_df_4["1931-10"][![enter image description here][1]][1] 但这不起作用: new_df_4["1931-10-02"] KeyError回溯最近一次呼叫last

在这种情况下,为什么我可以按月份进行选择,而不能按日期进行选择

dates = pd.date_range( start = "01/01/1931" ,  end  =  "01/02/1941" )
new_df_4 = new_df_3.reindex(dates)
new_df_4["1931-10"][![enter image description here][1]][1]
但这不起作用:

new_df_4["1931-10-02"]
KeyError回溯最近一次呼叫last 在里面 -->1新的_df_4[1931-10-02]

/Users/romain/anaconda/lib/python2.7/site-packages/pandas/core/frame.pyc in __getitem__(self, key)
   1990             return self._getitem_multilevel(key)
   1991         else:
-> 1992             return self._getitem_column(key)
   1993 
   1994     def _getitem_column(self, key):

/Users/romain/anaconda/lib/python2.7/site-packages/pandas/core/frame.pyc in _getitem_column(self, key)
   2002         result = self._constructor(self._data.get(key))
   2003         if result.columns.is_unique:
-> 2004             result = result[key]
   2005 
   2006         return result

/Users/romain/anaconda/lib/python2.7/site-packages/pandas/core/frame.pyc in __getitem__(self, key)
   1990             return self._getitem_multilevel(key)
   1991         else:
-> 1992             return self._getitem_column(key)
   1993 
   1994     def _getitem_column(self, key):

/Users/romain/anaconda/lib/python2.7/site-packages/pandas/core/frame.pyc in _getitem_column(self, key)
   1997         # get column
   1998         if self.columns.is_unique:
-> 1999             return self._get_item_cache(key)
   2000 
   2001         # duplicate columns & possible reduce dimensionality

/Users/romain/anaconda/lib/python2.7/site-packages/pandas/core/generic.pyc in _get_item_cache(self, item)
   1343         res = cache.get(item)
   1344         if res is None:
-> 1345             values = self._data.get(item)
   1346             res = self._box_item_values(item, values)
   1347             cache[item] = res

/Users/romain/anaconda/lib/python2.7/site-packages/pandas/core/internals.pyc in get(self, item, fastpath)
   3223 
   3224             if not isnull(item):
-> 3225                 loc = self.items.get_loc(item)
   3226             else:
   3227                 indexer = np.arange(len(self.items))[isnull(self.items)]

/Users/romain/anaconda/lib/python2.7/site-packages/pandas/indexes/base.pyc in get_loc(self, key, method, tolerance)
   1876                 return self._engine.get_loc(key)
   1877             except KeyError:
-> 1878                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   1879 
   1880         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4027)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3891)()

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12408)()

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12359)()

KeyError: '1931-10-02'
对于“按月选择”,请使用:

如果来自以下位置的分辨率相同,则此操作无效:

警告 但是,如果字符串被视为完全匹配,则 DataFrame的[]中的选择将按列而不是按行进行,请参阅 索引基础。例如,dft_分钟['2011-12-31 23:59']将升高 “2012-12-31 23:59”的KeyError与索引和 没有这样名称的列:始终具有明确的 选择,无论行被视为切片还是单个 选择,使用.loc

In [95]: dft_minute.loc['2011-12-31 23:59']
Out[95]: 
a    1
b    4
Name: 2011-12-31 23:59:00, dtype: int64
如果需要,可以使用“按日期选择”:

new_df_4.loc["1931-10-02"]
样本:

np.random.seed(10)
dates = pd.date_range( start = "01/01/1931" ,  end  =  "01/02/1941" )
new_df_4  = pd.DataFrame({'a':np.random.randint(10, size=len(dates))}, index=dates)
print (new_df_4.head())
            a
1931-01-01  9
1931-01-02  4
1931-01-03  0
1931-01-04  1
1931-01-05  9

print (new_df_4["1931-10"])
            a
1931-10-01  9
1931-10-02  6
1931-10-03  9
1931-10-04  7
1931-10-05  8
1931-10-06  0
1931-10-07  9
1931-10-08  6
1931-10-09  0
1931-10-10  1
1931-10-11  0
...

print (new_df_4.loc["1931-10-02"])
a    6
Name: 1931-10-02 00:00:00, dtype: int32
对于“按月选择”,请使用:

如果来自以下位置的分辨率相同,则此操作无效:

警告 但是,如果字符串被视为完全匹配,则 DataFrame的[]中的选择将按列而不是按行进行,请参阅 索引基础。例如,dft_分钟['2011-12-31 23:59']将升高 “2012-12-31 23:59”的KeyError与索引和 没有这样名称的列:始终具有明确的 选择,无论行被视为切片还是单个 选择,使用.loc

In [95]: dft_minute.loc['2011-12-31 23:59']
Out[95]: 
a    1
b    4
Name: 2011-12-31 23:59:00, dtype: int64
如果需要,可以使用“按日期选择”:

new_df_4.loc["1931-10-02"]
样本:

np.random.seed(10)
dates = pd.date_range( start = "01/01/1931" ,  end  =  "01/02/1941" )
new_df_4  = pd.DataFrame({'a':np.random.randint(10, size=len(dates))}, index=dates)
print (new_df_4.head())
            a
1931-01-01  9
1931-01-02  4
1931-01-03  0
1931-01-04  1
1931-01-05  9

print (new_df_4["1931-10"])
            a
1931-10-01  9
1931-10-02  6
1931-10-03  9
1931-10-04  7
1931-10-05  8
1931-10-06  0
1931-10-07  9
1931-10-08  6
1931-10-09  0
1931-10-10  1
1931-10-11  0
...

print (new_df_4.loc["1931-10-02"])
a    6
Name: 1931-10-02 00:00:00, dtype: int32

这是一个转折点,但并没有回答这个问题:-/@romainjouin它实际上不是一个解决方法,它是这样设计的,所以如果你想按索引选择,你需要使用.loc。请参见编辑中的警告。这是一个转折点,但没有回答问题:-/@romainjouin它实际上不是一个解决方法,它是这样设计的,因此如果要按索引选择,则需要使用.loc。有关警告,请参见编辑。