[英]how to convert datetime to milliseconds in python pandas
So I want to convert the datetime to millisecond my code is here 所以我想将日期时间转换为毫秒,我的代码在这里
import pandas as pd
import numpy as np
flyer = pd.read_csv("./csv/Flyers Dataset - Flyers Dataset.csv",parse_dates = ['timestamp'])
flyer.dropna(axis=0, how='any', thresh=None, subset=None, inplace=True)
pd.set_option('display.max_rows', 20)
flyer.to_csv('myfile.csv')
so the flyer variable is basically my entire dataframe(it's my csv file). 因此,flyer变量基本上是我的整个数据帧(这是我的csv文件)。 The csv https://i.stack.imgur.com/qLq2U.png
CSV https://i.stack.imgur.com/qLq2U.png
any sort of feedback will be greatly appreciated. 任何形式的反馈将不胜感激。 I am sorry about any of the formatting on this question this is only my second one.
对于这个问题的任何格式,我感到很抱歉,这只是我的第二个问题。
Sincerely, 此致
Gratefull StackOverflow User Gratefull StackOverflow用户
Given that the 'timestamp' column is a datetime object, and your base reference date is October 1 2018, here's how you can compute the timedelta in milliseconds 鉴于``时间戳''列是一个日期时间对象,并且您的基本引用日期是2018年10月1日,因此这是计算毫秒时间增量的方法
import pandas as pd
from datetime import datetime, timedelta, timezone
Firstly, get the timezone info from your sample timestamp input: 首先,从示例时间戳输入中获取时区信息:
datetime.strptime('2018-10-01 13:56:36-0400', '%Y-%m-%d %H:%M:%S%z')
>>>datetime.datetime(2018, 10, 1, 13, 56, 36, tzinfo=datetime.timezone(datetime.timedelta(-1, 72000)))
Then we can create a tz-aware base date with the same timezone details. 然后,我们可以创建具有相同时区详细信息的tz感知基准日期。 You should be able to do the subtraction after that.
之后,您应该可以进行减法。
base_date = datetime(2018,10,1, tzinfo=timezone(timedelta(-1,72000)))
flyer = pd.read_csv("./csv/Flyers Dataset - Flyers Dataset.csv",parse_dates = ['timestamp'])
flyer.dropna(axis=0, how='any', thresh=None, subset=None, inplace=True)
# 1000 milliseconds in a second
flyer.loc[:,'TimeDelta'] = flyer.loc[:,'timestamp'].apply(lambda x: (x - base_date).total_seconds() * 1000)
pd.set_option('display.max_rows', 20)
flyer.to_csv('myfile.csv')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.