Untitled


SUBMITTED BY: Guest

DATE: Dec. 11, 2013, 6:03 a.m.

FORMAT: Text only

SIZE: 2.8 kB

HITS: 840

  1. 29 04 13 18 15 00 7.667
  2. 29 04 13 18 30 00 7.000
  3. 29 04 13 18 45 00 7.000
  4. 29 04 13 19 00 00 7.333
  5. 29 04 13 19 15 00 7.000
  6. import pandas as pd
  7. from cStringIO import StringIO
  8. def parse_all_fields(day_col, month_col, year_col, hour_col, minute_col,second_col):
  9. day_col = _maybe_cast(day_col)
  10. month_col = _maybe_cast(month_col)
  11. year_col = _maybe_cast(year_col)
  12. hour_col = _maybe_cast(hour_col)
  13. minute_col = _maybe_cast(minute_col)
  14. second_col = _maybe_cast(second_col)
  15. return lib.try_parse_datetime_components(day_col, month_col, year_col, hour_col, minute_col, second_col)
  16. ##Read the .txt file
  17. data1 = pd.read_table('0132_3.TXT', sep='s+', names=['Day','Month','Year','Hour','Min','Sec','Value'])
  18. data1[:10]
  19. Out[21]:
  20. Day,Month,Year,Hour, Min, Sec, Value
  21. 29 04 13 18 15 00 7.667
  22. 29 04 13 18 30 00 7.000
  23. 29 04 13 18 45 00 7.000
  24. 29 04 13 19 00 00 7.333
  25. 29 04 13 19 15 00 7.000
  26. data2 = pd.read_table(StringIO(data1), parse_dates={'datetime':['Day','Month','Year','Hour''Min','Sec']}, date_parser=parse_all_fields, dayfirst=True)
  27. TypeError Traceback (most recent call last)
  28. <ipython-input-22-8ee408dc19c3> in <module>()
  29. ----> 1 data2 = pd.read_table(StringIO(data1), parse_dates={'datetime': ['Day','Month','Year','Hour''Min','Sec']}, date_parser=parse_all_fields, dayfirst=True)
  30. TypeError: expected read buffer, DataFrame found
  31. In [1]: df = pd.read_csv('0132_3.TXT', header=None, sep='s+s', parse_dates=[[0]])
  32. In [2]: df
  33. Out[2]:
  34. 0 1
  35. 0 2013-04-29 00:00:00 7.667
  36. 1 2013-04-29 00:00:00 7.000
  37. 2 2013-04-29 00:00:00 7.000
  38. 3 2013-04-29 00:00:00 7.333
  39. 4 2013-04-29 00:00:00 7.000
  40. In [11]: def date_parser(ss):
  41. day, month, year, hour, min, sec = ss.split()
  42. return pd.Timestamp('20%s-%s-%s %s:%s:%s' % (year, month, day, hour, min, sec))
  43. In [12]: df = pd.read_csv('0132_3.TXT', header=None, sep='s+s', parse_dates=[[0]], date_parser=date_parser)
  44. In [13]: df
  45. Out[13]:
  46. 0 1
  47. 0 2013-04-29 18:15:00 7.667
  48. 1 2013-04-29 18:30:00 7.000
  49. 2 2013-04-29 18:45:00 7.000
  50. 3 2013-04-29 19:00:00 7.333
  51. 4 2013-04-29 19:15:00 7.000

comments powered by Disqus