确保熊猫read_csv遵循给定的dtypes

时间:2018-08-31 12:50:58

标签: python pandas

我有以下csv数据,public class Controller { public HeaderObject HeaderObj; public Main_Form FormMain; Public Controller(Main_Form pFormMain) { FormMain = pFormMain; } ...... foreach(var sheet in ExcelSheets) { PrintToConsole(sheet + " - Processing Data"); if (formPreferences.CheckEverySheet.Checked) { HeaderObj = null; // set to null each time we process a new sheet FormMain.formSheetChecker = new Form_Headers(lFormMain, this); //pass a reference to Controller class here QuickLoadExcelData(lExcelWorkSheet); FormMain.formSheetChecker.StartPosition = FormStartPosition.CenterScreen; FormMain.formSheetChecker.ShowDialog(); //do some processing here and then pass back object to controller class before form //before the formSheetChecker is closed it sets the HeaderObj in the controller class as i have passed a reference to the controller class in the //formSheetCheckers constructor. if (HeaderObj != null) { storageObject.setHeader(lExcelWorkSheet.Name, HeaderObj); //i get a NullReferenceException here even when there is data inside the object } else { HeaderObj = new HeaderObject("failed", -1); storageObject.setHeader(lExcelWorkSheet.Name, HeaderObj); } } } } public partial class Form_Headers : Form { Form_Main FormMain; Controller controller; HeaderObject headerObject = null; string lSheetName = ""; public Form_Headers(Form_Main pFormMain, Controller pController) { InitializeComponent(); FormMain = pFormMain; controller = pController; headerObject = controller.HeaderObj; } public void UpdateHeaderRange() { if (!(headerObject.Variable1 == "")) { //do nothing here } else { headerObject.SetHeaderFields("failed", -1); } } private void button_Save_Click(object sender, EventArgs e) { UpdateHeaderRange(); this.Close(); } 。请注意,csv文件格式正确,并且不包含任何“怪异”的东西。

data.csv

我通过以下方式阅读它:

id,date,time,name,value,description
0,2017-01-07,2017-01-07 11:49:29,cool_name1,2,something
1,2017-01-07,2017-01-07 11:51:50,cool_name1,-1,something
2,2017-01-07,2017-01-07 14:03:42,cool_name2,9,something

结果为schema = {'id': float, 'date': str, 'time': str, 'name': str, 'value': float, 'description': str} df = pd.read_csv('data.csv', header=0, dtype=schema, parse_dates=['date', 'time'])

df.dtypes

由于数据的去向,id int64 date datetime64[ns] time datetime64[ns] name object value float64 description object 必须按照id(在这种情况下为schema)的指定来自动解析。但是,在尝试了一堆不同的关键字参数并相互结合之后,我无法让float服从我的id

我尝试过的内容的列表:

dtype

应注意,列dtype=schema converters=schema low_memory=False engine='c' AND 'python' index_col=False parse_dates=False 确实服从value,并且没有指定它就变成dtype

缺少的成分是什么(熊猫版0.23.4)?

0 个答案:

没有答案