2 回答
TA贡献2011条经验 获得超2个赞
您可以使用 viewcasting 来访问单个字符:
time_strings[...,None].view('U1')[...,10] = ','
time_strings
就地改变。
TA贡献1816条经验 获得超4个赞
In [309]: np.char.replace(time_strings,'T',',')
Out[309]:
array(['1970-01-01,00:00:00.000', '1970-01-01,00:00:00.001',
'1970-01-01,00:00:00.002', '1970-01-01,00:00:00.003',
'1970-01-01,00:00:00.004', '1970-01-01,00:00:00.005',
'1970-01-01,00:00:00.006', '1970-01-01,00:00:00.007',
....
但是@PaulPanzer 的就地速度要快得多(即使它有点晦涩难懂):
In [316]: %%timeit temp=time_strings.copy()
...: temp[...,None].view('U1')[...,10] = ','
8.48 µs ± 34.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [317]: timeit np.char.replace(time_strings,'T',',')
1.23 ms ± 1.12 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
添加回答
举报