问题
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
回答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
回答2:
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)
来源:https://stackoverflow.com/questions/48936176/how-to-load-a-file-with-date-and-time-as-a-datetime-object-in-python