Home > Net >  pandas dataframe timestamp conversion
pandas dataframe timestamp conversion

Time:06-21

I have dataset written in YYYY-MM-DD HH:MM:SS, I want to convert timestamp(YYYY-MM-DD HH:MM:SS) to UNIX timestamp (ex. 1655805822).

so,

df['Timestamp'] = df['Timestamp'].astype(np.int64) // 1000000

then, error occurs,

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3444, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-32d4e584f08d>", line 1, in <module>
    runfile('C:/Users/Monet/PycharmProjects/pythonProject1/to_image.py', wdir='C:/Users/Monet/PycharmProjects/pythonProject1')
  File "C:\Program Files\JetBrains\PyCharm 2022.1\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 198, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2022.1\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents "\n", file, 'exec'), glob, loc)
  File "C:/Users/Monet/PycharmProjects/pythonProject1/to_image.py", line 26, in <module>
    df['Timestamp'] = df['Timestamp'].astype(np.int64) // 1000000
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py", line 5815, in astype
    new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals\managers.py", line 418, in astype
    return self.apply("astype", dtype=dtype, copy=copy, errors=errors)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals\managers.py", line 327, in apply
    applied = getattr(b, f)(**kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals\blocks.py", line 591, in astype
    new_values = astype_array_safe(values, dtype, copy=copy, errors=errors)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\dtypes\cast.py", line 1309, in astype_array_safe
    new_values = astype_array(values, dtype, copy=copy)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\dtypes\cast.py", line 1257, in astype_array
    values = astype_nansafe(values, dtype, copy=copy)
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\dtypes\cast.py", line 1174, in astype_nansafe
    return lib.astype_intsafe(arr, dtype)
  File "pandas\_libs\lib.pyx", line 679, in pandas._libs.lib.astype_intsafe
ValueError: invalid literal for int() with base 10: '2013-11-01 00:10:00'

CodePudding user response:

You need convert it to datetime first

df['Timestamp'] = pd.to_datetime(df['Timestamp']) # <--here
df['Timestamp'] = df['Timestamp'].astype(np.int64) // 1000000
  • Related