0

The objective of this post is to be able to convert the columns [‘Open Date’, 'Close date’] to timestamp format

I have tried with the functions / examples from these links with any results.

Convert datetime to timestamp in Neo4j

Convert datetime pandas

Pandas to_dict() converts datetime to Timestamp

Really appreciate any ideas / comments / examples on how to do so.

Data Base Image

Column Characteristics:

Open Date datetime64[ns] and pandas.core.series.Series

Close date datetime64[ns] and pandas.core.series.Series

Finally I been using these libraries

import pandas as pd

import numpy as np

from datetime import datetime, date, time, timedelta

1 Answers1

0

You convert first to numpy array by values and transform (cast) to int64 - output is in nanoseconds , which means divide by 10 ** 9:

df['open_ts'] = df['Open_Date'].datetime.values.astype(np.int64)
df['close_ts'] = df['Close_Date'].datetime.values.astype(np.int64)

OR

If you want to avoid using numpy, you can also try:

df['open_ts'] = pd.to_timedelta(df['Open_Date'], unit='ns').dt.total_seconds().astype(int)
df['close_ts'] = pd.to_timedelta(df['Close_Date'], unit='ns').dt.total_seconds().astype(int)

Try them and report it back here

Lorenzo Bassetti
  • 795
  • 10
  • 15
  • 1
    Hi, thank you very much for your help, on the next images the errors ("'Series' object has no attribute 'datetime'" and "dtype datetime64[ns] cannot be converted to timedelta64[ns]") that I got when running the different options, what could I be doing wrong? https://postimg.cc/gallery/G8djr4z – holguinmora Nov 24 '21 at 15:09
  • Ok, you have to first create a DF with your series: https://pandas.pydata.org/docs/reference/api/pandas.Series.to_frame.html Then, you can use the code I gave you so you can convert the columns you need. – Lorenzo Bassetti Nov 24 '21 at 18:15