1

need to load this file with date in first col and HH:MM in second col.

How does it work with a numpy.genfromtxt()? Maybe pandas?

My file looks like:

2017-Feb-11 00:00  m    4.87809   1.86737   5.04236   0.27627   1.5995 
2017-Feb-11 00:05  m    4.86722   1.86711   5.00023   0.27616   1.5965 
2017-Feb-11 00:10  m    4.85641   1.86690   4.95810   0.27604   1.5941 
hawk
  • 21
  • 4
  • 2
    What have you tried? What isn't working as you would expect? Without researching I think numpy and pandas can do this for you. – djhoese Feb 22 '18 at 20:00
  • tried genfromtxt, got nan for col 1 + 2 and didn´t work converting in datetime object. Formatting string didn´t work too. – hawk Feb 23 '18 at 13:37
  • Well good thing MaxU gave you an answer. – djhoese Feb 23 '18 at 17:06

2 Answers2

1
In [32]: df = pd.read_csv(filename, delim_whitespace=True, parse_dates=[0], header=None)

In [33]: df[1] = pd.to_timedelta(df[1] + ':00')

In [34]: df
Out[34]:
           0        1  2        3        4        5        6       7
0 2017-02-11 00:00:00  m  4.87809  1.86737  5.04236  0.27627  1.5995
1 2017-02-11 00:05:00  m  4.86722  1.86711  5.00023  0.27616  1.5965
2 2017-02-11 00:10:00  m  4.85641  1.86690  4.95810  0.27604  1.5941

In [35]: df.dtypes
Out[35]:
0     datetime64[ns]
1    timedelta64[ns]
2             object
3            float64
4            float64
5            float64
6            float64
7            float64
dtype: object

PS you can't have datetime dtype without a date component in Pandas - that's why I've converted it to timedelta dtype. Alternatively you could combine first two columns in a single datetime column:

In [29]: df['date'] = pd.to_datetime(df.pop(0) + ' ' + df.pop(1) + ':00')

In [30]: df
Out[30]:
   2        3        4        5        6       7                date
0  m  4.87809  1.86737  5.04236  0.27627  1.5995 2017-02-11 00:00:00
1  m  4.86722  1.86711  5.00023  0.27616  1.5965 2017-02-11 00:05:00
2  m  4.85641  1.86690  4.95810  0.27604  1.5941 2017-02-11 00:10:00

In [31]: df.dtypes
Out[31]:
2               object
3              float64
4              float64
5              float64
6              float64
7              float64
date    datetime64[ns]
dtype: object
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
0

import pandas ? No, use the numpy.genfromtxt() tool better:

Well, loading a full, heavyweight package just for this conversion is possible, but quite awkward.

As your other question already started to inspect the numpy.genfromtxt() powers, keep walking:

converters  = { 0: lambda aSTR: datetime.datetime.strptime( aSTR, "%Y-%b-%d" ),
                #  WARNING:                                            ^
                #                                                      |
                #  above presented conversion specifier is locale-dependent
                #                                       so revise code if used
                #                                       in other locale domains
                1: lambda aSTR: getTimeDelta( aSTR ),
                }

and finally, use a common practice of .datetime + .timedelta aritmetics.

Loading a fully-fledged pandas just for adding two columns of datetime value is possible, but a bit [TIME]-domain and [SPACE]-domain overkill, isn't it?

:o)

user3666197
  • 1
  • 6
  • 50
  • 92