我正在尝试从以下 URL 将 excel 文件读入 Pandas:
url1 = 'https://cib.societegenerale.com/fileadmin/indices_feeds/CTA_Historical.xls'
url2 = 'https://cib.societegenerale.com/fileadmin/indices_feeds/STTI_Historical.xls'
Run Code Online (Sandbox Code Playgroud)
使用代码:
pd.read_excel(url1)
Run Code Online (Sandbox Code Playgroud)
但是它不起作用,我收到错误:
XLRDError: Unsupported format, or corrupt file: Expected BOF record; found '2000/01/'
Run Code Online (Sandbox Code Playgroud)
在 Google 上搜索后,似乎有时通过 URL 提供的 .xls 文件实际上在幕后以不同的文件格式保存,例如 html 或 xml。
当我手动下载 Excel 文件并使用 Excel 打开它时,出现错误消息:文件格式和扩展名不匹配。该文件可能已损坏或不安全。除非你相信它的来源,否则不要打开它”
当我打开它时,它看起来就像一个普通的 Excel 文件。
我在网上看到一篇文章,建议我在文本编辑器中打开该文件,看看是否有任何关于正确文件格式的附加信息,但使用记事本++打开时我没有看到任何附加信息。
有人可以帮我正确地将这个“xls”文件读入 pandas DataFramj 吗?
我目前正在尝试迭代SQL请求游标中包含的一些数据,将某些数据的类型更改为"datetime.time",然后将其与另一个变量组合成一个名为"datetime_db"的新变量.
我有两个名为"date"和"nextDay"的变量,这些变量之前已在我的代码中定义过.前面提到的"datetime.time"将与"date"或"nextDay"组合,具体取决于某些条件.
我的代码如下:
for (date_db,time_db,price) in cursor:
time_db = datetime.datetime.strptime(time_db,"%H:%M:%S").time()
price = float(price)
if (date_db == date):
datetime_db = datetime.datetime.combine(datetime.date(date), datetime.time(time_db))
else:
datetime_db = datetime.datetime.combine(datetime.date(nextDay), datetime.time(time_db))
Run Code Online (Sandbox Code Playgroud)
这会引发以下错误:
File "C:/Users/Stuart/PycharmProjects/untitled/Apache - Copy.py", line 82, in <module>
datetime_db = datetime.datetime.combine(datetime.date(date), datetime.time(time_db))
TypeError: an integer is required
Run Code Online (Sandbox Code Playgroud)
当我为所涉及的3个变量打印出"type()"时,我得到以下内容:
time_db = <type 'datetime.time'> date = <type 'datetime.datetime'> nextDay = <type 'datetime.datetime'>
Run Code Online (Sandbox Code Playgroud)
有什么明显的原因导致这种情况无效吗?我已经尝试将"date"和"nextDay"的类型更改为"datetime.date",但这没有任何区别.
有人可以建议我如何成功地将这两个变量结合起来吗?
我想查询一个mySQL表,在两个日期和两个日期之间提取数据.我知道如何使用"之间"调用为单个"日期时间"列执行此操作,但我的列是一个"日期"列和一个"时间"列.我在网上找到的所有解决方案都是针对单个日期时间列.
我的范围从15:30的"day1"到15:14的day1 + 1day
到目前为止,我可以得到以下范围(有效):
SELECT time,
close
FROM intraday_values
WHERE date="2005-03-01"
and time between "15:30" and "23:59"
Run Code Online (Sandbox Code Playgroud)
但我显然需要合并2个日期和两个日期.我尝试了以下但得到一个错误:
SELECT time,
close
FROM intraday_values
between date="2005-03-01"
and time="15:30"
and date="2005-03-02"
and time = "15:14"
Run Code Online (Sandbox Code Playgroud)
有人可以帮我正确地制定查询吗?非常感谢
我使用Python进行pyodbc导入.
我使用的是Microsoft Office 2013 64bit.
我正在尝试查询accdb数据库以选择范围内的不同日期并将它们分配给游标,以便我可以将它们附加到列表中.
我的Access数据库有一个名为Closing_prices的表,以及一个名为Date_的列,其数据类型为"Date/Time".
我的代码如下:
cursor=conx.cursor()
query="select distinct Date_ FROM Closing_prices where Date_ >= '10/8/2011' and Date_ < '30/04/2014'"
cursor.execute(query)
dates=list()
for date in cursor:
dates.append(date[0])
Run Code Online (Sandbox Code Playgroud)
但是我收到错误消息:
Traceback (most recent call last):
File "C:/Users/Stuart/PycharmProjects/untitled/Apache - Copy.py", line 20, in <module>
cursor.execute(query)
pyodbc.Error: ('07002', '[07002] [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1. (-3010) (SQLExecDirectW)')
Run Code Online (Sandbox Code Playgroud)
由于Date_是一个日期时间,我也尝试过:
query="select distinct Date_ FROM Closing_prices where Date_ >= '10/8/2011 00:00:00' and Date_ < '30/04/2014 00:00:00'"
Run Code Online (Sandbox Code Playgroud)
当我跑:
cursor = conx.cursor()
query="select Date_ …Run Code Online (Sandbox Code Playgroud) 我有一个pandas DataFrame(名为"df1")具有以下结构(虽然我有很多个月的日常数据):
date WeightedReturn
0 15/07/2015 0.005128
1 15/07/2015 0.002844
2 15/07/2015 0.003055
3 15/07/2015 -0.001481
4 15/07/2015 -0.000741
5 15/07/2015 -0.000741
6 16/07/2015 -0.004253
7 16/07/2015 -0.001712
8 16/07/2015 -0.001712
9 21/07/2015 -0.000178
10 21/07/2015 -0.000089
11 21/07/2015 -0.00008
Run Code Online (Sandbox Code Playgroud)
我希望从中创建一个新的DataFrame,它充当数据透视表并合并日期字段并对该特定日期的加权回报求和,得到如下内容:
date WeightedReturn
0 15/07/2015 0.00806425
1 16/07/2015 -0.007676
2 21/07/2015 -0.000356
Run Code Online (Sandbox Code Playgroud)
我尝试过使用"groupby函数":
df2 = df1.groupby('date').sum()
Run Code Online (Sandbox Code Playgroud)
这种(有点)工作,但输出然后错误地排序日期,如下所示:
WeightedReturn
date
01/09/2015 0.004803
02/09/2015 0.005144
03/08/2015 -0.000120
03/09/2015 -0.025164
04/08/2015 0.003956
04/09/2015 0.008942
05/08/2015 -0.01323
Run Code Online (Sandbox Code Playgroud)
你可以看到的不是按时间顺序排列的.
所以我尝试使用数据透视表功能,但是阅读有关它使用的文档时我感到非常困惑.
我试过了:
df2 = pandas.pivot_table(df1, values="Weighted …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 matplotplib 绘制烛台图。我已经查询了我的数据库,返回了相关数据并以所需的格式(日期、开盘价、收盘价、最高价、最低价)附加到了一个名为 candleAr 的数组中
我的代码如下:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
from matplotlib.finance import candlestick
candleAr=[]
cursor = conx.cursor()
query= 'SELECT ticker,date,time,open,low,high,close FROM eurusd WHERE date > "2013-02-28"'
cursor.execute(query)
for line in cursor:
#appendLine in correct format for candlesticks - date,open,close,high,low
appendLine = line[1],line[3],line[6],line[5],line[4]
candleAr.append(appendLine)
fig = plt.figure()
ax1 = plt.subplot(1,1,1)
candlestick(ax1, candleAr, width=1, colorup='g', colordown='r')
ax1.grid(True)
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误消息:
Traceback (most recent call last):
File "C:\Users\Stuart\Desktop\Python Programming\Apache\Liclipse\Andres-Apache\FX\fx2.py", …Run Code Online (Sandbox Code Playgroud) python ×5
pandas ×2
sql ×2
dataframe ×1
datetime ×1
matplotlib ×1
ms-access ×1
mysql ×1
pivot-table ×1
pyodbc ×1
python-2.7 ×1
xlrd ×1