<p>使用字符串操作提取小时数<code>zfill</code>最多4个字符(如果还有秒,则为6个),然后对前2个字符进行切片以获得小时数(分钟为[2:4],秒为[4:6])。使用<code>pd.to_numeric</code>获取数字数据类型</p>
<pre><code>df['hour'] = pd.to_numeric(df['time'].astype(str).str.zfill(4).str[0:2])
df['minutes'] = pd.to_numeric(df['time'].astype(str).str.zfill(4).str[2:4])
invoiceNo time invoiceValue hour minutes
0 A 6 2 0 6
1 B 12 3 0 12
2 C 356 5 3 56
3 D 2145 6 21 45
</code></pre>
<hr/>
<p>如果您有兴趣将<code>'time'</code>转换为<code>timedelta64[ns]</code>数据类型,您可以使用<code>pd.to_datetime</code>的灵活解析。由于缺少年/月/日,因此默认值为1900-01-01,我们将其减去</p>
<pre><code>df['new_time'] = (pd.to_datetime(df['time'].astype(str).str.zfill(4), format='%H%M')
- pd.to_datetime('1900-01-01'))
invoiceNo time invoiceValue hour minutes new_time
0 A 6 2 0 6 0 days 00:06:00
1 B 12 3 0 12 0 days 00:12:00
2 C 356 5 3 56 0 days 03:56:00
3 D 2145 6 21 45 0 days 21:45:00
</code></pre>