Thank you all for your time in advance. I have a number of space delimited text files in the format;
29 04 13 18 15 00 7.667
29 04 13 18 30 00 7.000
29 04 13 18 45 00 7.000
29 04 13 19 00 00 7.333
29 04 13 19 15 00 7.000
being in the format DD MM YY HH MM SS and my result value. I am trying to read the txt file using Python's pandas. I have tried doing quite a bit of research on this prior to posting this question so hope I am not covering trodden ground.
Based on trial and error and research I have come up with:
import pandas as pd
from cStringIO import StringIO
def parse_all_fields(day_col, month_col, year_col, hour_col, minute_col,second_col):
day_col = _maybe_cast(day_col)
month_col = _maybe_cast(month_col)
year_col = _maybe_cast(year_col)
hour_col = _maybe_cast(hour_col)
minute_col = _maybe_cast(minute_col)
second_col = _maybe_cast(second_col)
return lib.try_parse_datetime_components(day_col, month_col, year_col, hour_col, minute_col, second_col)
##Read the .txt file
data1 = pd.read_table('0132_3.TXT', sep='\s+', names=['Day','Month','Year','Hour','Min','Sec','Value'])
data1[:10]
Out[21]:
Day,Month,Year,Hour, Min, Sec, Value
29 04 13 18 15 00 7.667
29 04 13 18 30 00 7.000
29 04 13 18 45 00 7.000
29 04 13 19 00 00 7.333
29 04 13 19 15 00 7.000
data2 = pd.read_table(StringIO(data1), parse_dates={'datetime':['Day','Month','Year','Hour''Min','Sec']}, date_parser=parse_all_fields, dayfirst=True)
TypeError Traceback (most recent call last)
<ipython-input-22-8ee408dc19c3> in <module>()
----> 1 data2 = pd.read_table(StringIO(data1), parse_dates={'datetime': ['Day','Month','Year','Hour''Min','Sec']}, date_parser=parse_all_fields, dayfirst=True)
TypeError: expected read buffer, DataFrame found
At this point I am stuck. Firstly the expected read buffer error confuses me. Do I need to do more pre-processing of the .txt file to get the dates into a readable format? Note - the parse_function of read_table does not work on its own on this date format.
I am a beginner - trying to learn. Sorry if the code is wrong/basic/confusing. Would be very appreciative if someone could help. Many thanks in advance.