Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pandas dataframe timestamp conversion

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,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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'

>Solution :

You need convert it to datetime first

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading