Home > Net >  python pandas convert date to milliseconds
python pandas convert date to milliseconds

Time:12-05

I would like to know better way to convert date to milliseconds using only Pandas.

start_date = '2021-10-01 00:00:00'
start_date = pd.Timestamp(start_date)
print(start_date.timestamp()*1000)

return:

1633046400000.0

I would like to get the following variable without string operation:

1633046400000

How to convert '2021-10-01 00:00:00' smarter?

CodePudding user response:

There is no millisecond output as string by default. As you have figured out, you might manipulate by numerical operations from float to string or you can use strftime;

start_date.strftime('%s%f')[:-3]

However %f formatting outputs microseconds (000000-999999) which you can truncate with a simple string manipulation to milliseconds.

Output

1633046400000 <class 'str'>

CodePudding user response:

You can wrap int around float variable convert int to float

start_date = '2021-10-01 00:00:00'
start_date = pd.Timestamp(start_date)
print(int(start_date.timestamp()*1000))

Output: 1633046400000

  • Related