使用python .csv's
模块将其中一个pd.DataFrame
转换为odo
时,我会收到TypeError
TypeError: Cannot cast array from dtype('float64') to dtype('int64')
according to the rule 'safe'
适用于其他csv's
# csv table file name
csvNm = 'table.csv'
# convert mysql table to csv
odo_csv = odo(tstConn.connect_string + '::' + tbl , csvNm)
# convert csv to pandas
odo_df = odo(odo_csv , pd.DataFrame)
这是我到目前为止所做的尝试无效:
import pandas as pd
from odo import odo, resource, discover, convert
odo_csv=odo(tstConn.connect_string + '::' + tbl , csvNm)
csv=resource(csvNm)
ds=discover(csv)
# Convert csv to pandas
odo_df = odo(odo_csv , pd.DataFrame, dshape=ds)
和此:
odo_df = odo(odo_csv , pd.DataFrame, casting='unsafe')
更新1 看起来我忽略了这个错误中最明显的暗示
pandas\parser.pyx in pandas.parser.TextReader._convert_tokens (pandas\parser.c:11816)()
导致Windows SO中的编码问题。 但这不是:
odo_df = odo(odo_csv , pd.DataFrame, encoding=odo_csv.encoding)
或这项工作
odo_df = odo(odo_csv , pd.DataFrame, encoding='cp1252')
这种不合时宜的方式(针对我的用例)来自pandas-reading-csv-files(与上面相同的链接)
# Python3
with open('/tmp/test.csv', 'r', encoding='cp1252') as f:
df = pd.read_csv(f)
print(df)
不确定下一步该尝试,我们将不胜感激。
答案 0 :(得分:0)
有效的解决方案是:
import pandas as pd
from odo import odo, resource, discover, convert
# convert mysql to csv
odo_csv=odo(raw_dbConn.connect_string + '::' + tblName , csvNm, header=True)
# Get odo resource aka sqlalchemy.Table instance
resc=resource(raw_dbConn.connect_string + '::' + tblName )
# Discover the resc
ds=discover(resc)
# Convert csv to dataframe
odo_df = odo(odo_csv , pd.DataFrame, dshape=ds ,encoding=odo_csv.encoding)