Aleksandrs Frolovs
Aleksandrs Frolovs

Reputation: 31

Python convert string to date in dataframe

I kindly ask you to help me with the following

I have a dataframe of string that I would like to convert to dates. Unfortunately, system generates an error

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)

Error

 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: ''

Upvotes: 0

Views: 209

Answers (1)

MrNobody33
MrNobody33

Reputation: 6483

It seems like you forgot to add the row(x) that it's being modified:

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)

Also, you could try to use the function of pandas pd.to_datetime instead of apply:

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

Both outputs:

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

Upvotes: 2

Related Questions