简体   繁体   English

如何在python熊猫中将日期时间转换为毫秒

[英]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.

相关问题 如何在python / pandas的毫秒部分转换带逗号(,)的datetime-String? - How to convert a datetime-String with a comma (,) in the milliseconds part in python/pandas? 在 Pandas 中,如何将字符串转换为以毫秒为单位的日期时间对象? - In Pandas, how to convert a string to a datetime object with milliseconds? 如何在python或更好的替代方案中将毫秒转换为日期时间? - How to Convert milliseconds to datetime in python or better alternative? pandas python 如何将持续时间转换为毫秒? - pandas python how to convert time duration to milliseconds? 以毫秒为单位将 python 日期时间转换为时间戳 - Convert python datetime to timestamp in milliseconds 如何将日期时间对象转换为毫秒 - How to convert datetime object to milliseconds 如何将毫秒的时间戳转换为日期时间? - How to convert timestamp with milliseconds into datetime? 如何将 csv 带毫秒的日期和时间转换为带毫秒的日期时间 - How to convert csv date and time with milliseconds to datetime with milliseconds 如何将字符串转换为具有null的datetime-python,pandas? - How to convert string to datetime with nulls - python, pandas? 如何在 pandas python 中将字符串转换为日期时间格式? - How to convert string to datetime format in pandas python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM