Xia*_*hao 1 indexing dataframe pandas
我收到一个TypeError:
TypeError: '<' not supported between instances of 'datetime.date' and 'str'`
Run Code Online (Sandbox Code Playgroud)
在运行以下代码时:
import requests
import re
import json
import pandas as pd
def retrieve_quotes_historical(stock_code):
quotes = []
url = 'https://finance.yahoo.com/quote/%s/history?p=%s' % (stock_code, stock_code)
r = requests.get(url)
m = re.findall('"HistoricalPriceStore":{"prices":(.*?),"isPending"',
r.text)
if m:
quotes = json.loads(m[0])
quotes = quotes[::-1]
return [item for item in quotes if not 'type' in item]
quotes = retrieve_quotes_historical('INTC')
df=pd.DataFrame(quotes)
s=pd.Series(pd.to_datetime(df.date,unit='s'))
df.date=s.dt.date
df=df.set_index('date')
Run Code Online (Sandbox Code Playgroud)
这段代码运行得很顺利,但是当我尝试运行这段代码时:
df['2017-07-07':'2017-07-10']
Run Code Online (Sandbox Code Playgroud)
我得到TypeError。
谁能帮我?
问题是'2017-07-07'
当索引类型为datetime.date时,您想使用Strings进行切片。您的切片也应该属于这种类型。
您可以通过如下定义开始日期和结束时间来做到这一点:
import pandas as pd
startdate = pd.to_datetime("2017-7-7").date()
enddate = pd.to_datetime("2017-7-10").date()
df.loc[startdate:enddate]
Run Code Online (Sandbox Code Playgroud)
startdate和enddate现在为datetime.date类型,您的分片将起作用:
adjclose close high low open volume
date
2017-07-07 33.205006 33.880001 34.119999 33.700001 33.700001 18304500
2017-07-10 32.979588 33.650002 33.740002 33.230000 33.250000 29918400
Run Code Online (Sandbox Code Playgroud)
也可以创建没有熊猫的datetime.date类型:
import datetime
startdate = datetime.datetime.strptime('2017-07-07', "%Y-%m-%d").date()
enddate = datetime.datetime.strptime('2017-07-10', "%Y-%m-%d").date()
Run Code Online (Sandbox Code Playgroud)
除了Paul的回答之外,还有几点需要注意:
\npd.to_datetime(df[\'date\'],unit=\'s\')
已经返回 aSeries
所以你不需要包装它。
此外,当解析成功时,返回的值为Series
datetime64 [ns] (timezone-na\xc3\xafve) 或 datetime64[ns, tz] (timezone-aware)。如果解析失败,它仍然可能返回一个没有错误的 Series,其 dtype为“object”(至少在 pandas 1.2.4 中),表示回退到 Python 的 stdlib 。pd.to_datetime
dtype
O
datetime.datetime
使用字符串进行过滤,仅当索引的 为df[\'2017-07-07\':\'2017-07-10\']
时才有效,而当索引为(objectdtype
datetime64[...]
O
因此,有了所有这些,您的示例只需更改最后几行即可运行:
\ndf = pd.DataFrame(quotes)\ns = pd.to_datetime(df[\'date\'],unit=\'s\') # no need to wrap in Series\nassert str(s.dtype) == \'datetime64[ns]\' # VERY IMPORTANT!!!!\ndf.index = s\nprint(df[\'2020-08-01\':\'2020-08-10\']) # it now works!\n
Run Code Online (Sandbox Code Playgroud)\n它产生:
\ndf = pd.DataFrame(quotes)\ns = pd.to_datetime(df[\'date\'],unit=\'s\') # no need to wrap in Series\nassert str(s.dtype) == \'datetime64[ns]\' # VERY IMPORTANT!!!!\ndf.index = s\nprint(df[\'2020-08-01\':\'2020-08-10\']) # it now works!\n
Run Code Online (Sandbox Code Playgroud)\n最后还要注意,如果您的日期时间格式以某种方式包含时间偏移量,则似乎有一个强制utc=True
参数需要添加(在 Pandas 1.2.4 中)到pd.to_datetime
,否则即使解析成功,返回的 dtype 也将是“O”。我希望这在未来会有所改善,因为它根本不直观。
有关详细信息,请参阅to_datetime
文档。