Python将字符串转换为数据帧中的日期

时间:2020-07-27 14:50:34

标签: python-3.x pandas dataframe

请您在以下方面为我提供帮助

我有一个要转换为日期的字符串数据框。不幸的是,系统生成错误

Dataframe 
  ID                    col_1
0  1  \/Date(1529424891295)\/
1  2  \/Date(1529424891295)\/
2  3  \/Date(1529424891295)\/

def convert_dates(timestamp_str):
    timestamp2 = datetime.datetime.fromtimestamp(int(timestamp_str[7:20])/1000)
    timestamp3 = timestamp2.strftime("%Y-%m-%dT%H:%M:%SZ")
    return timestamp3

df['col_3'] = df.apply(lambda x: convert_dates('col_1'), axis=1)

错误

 df['col_3'] = df.apply(lambda x: convert_dates('col_1'), axis=1)
  File "C:\Users\103925alf1\AppData\Roaming\Python\Python38\site-packages\pandas\core\frame.py", line 6878, in apply
    return op.get_result()
  File "C:\Users\103925alf1\AppData\Roaming\Python\Python38\site-packages\pandas\core\apply.py", line 186, in get_result
    return self.apply_standard()
  File "C:\Users\103925alf1\AppData\Roaming\Python\Python38\site-packages\pandas\core\apply.py", line 295, in apply_standard
    result = libreduction.compute_reduction(
  File "pandas\_libs\reduction.pyx", line 620, in pandas._libs.reduction.compute_reduction
  File "pandas\_libs\reduction.pyx", line 128, in pandas._libs.reduction.Reducer.get_result
  File "C:/Users/103925alf1/PycharmProjects/p09/p09.py", line 21, in <lambda>
    df['col_3'] = df.apply(lambda x: convert_dates('col_1'), axis=1)
  File "C:/Users/103925alf1/PycharmProjects/p09/p09.py", line 8, in convert_dates
    timestamp2 = datetime.datetime.fromtimestamp(int(timestamp_str[7:20])/1000)
ValueError: invalid literal for int() with base 10: ''

1 个答案:

答案 0 :(得分:1)

似乎您忘了添加正在被修改的行(x):

def convert_dates(timestamp_str):
    timestamp2 = datetime.datetime.fromtimestamp(int(timestamp_str[7:20])/1000)
    timestamp3 = timestamp2.strftime("%Y-%m-%dT%H:%M:%SZ")
    return timestamp3

df['col_3'] = df.apply(lambda x: convert_dates(x['col_1']), axis=1)

此外,您可以尝试使用熊猫pd.to_datetime的功能,而不是应用:

df['col_3']=pd.to_datetime(df['col_1'].str[7:20].astype('int64'),unit='ms').dt.strftime("%Y-%m-%dT%H:%M:%SZ")

两个输出:

df
   ID                    col_1                 col_3
0   1  \/Date(1529424891295)\/  2018-06-19T16:14:51Z
1   2  \/Date(1529424891295)\/  2018-06-19T16:14:51Z
2   3  \/Date(1529424891295)\/  2018-06-19T16:14:51Z