I have a data file in the form of
Day Time Value
20100511,001012,8
20100511,001052,14
I load the file in a SciPy
script with
data = np.loadtxt("data.txt", delimiter=",", usecols=(0,1,2))
y= data[:,2]
How can I make x = timestamp
from the data given in the first two columns?
答案 0 :(得分:3)
使用Pandas很容易。首先,解析CSV:
scipy.optimize.leastsq
然后:
data = pd.read_csv('data.txt', skiprows=1, names=['Day', 'Time', 'Value'], dtype=str)
这给了你:
pd.to_datetime(data.Day + 'T' + data.Time) # ISO 8601 compact format
或者,发烧友,一步到位:
0 2010-05-11 00:10:12
1 2010-05-11 00:10:52
dtype: datetime64[ns]
答案 1 :(得分:0)
假设您不想使用pandas,可以使用numpy.apply_along_axis
:
In [77]: np.apply_along_axis(lambda x: datetime.strptime(x[0][2:-1] + x[1][2:-1], "%Y%m%d%H%M%S"), 1, data[1:])
Out[77]:
array([datetime.datetime(2010, 5, 11, 0, 10, 12),
datetime.datetime(2010, 5, 11, 0, 10, 52)], dtype=object)
笨拙的字符串切片,因为np.read_txt
在开头添加了额外的b'
,在结尾添加了'
。