[英]Strip microsecond from datetime
For a given timedata - 2018-06-01 06:36:40.047883+00:00
, I want to remove microsecond and strip the value after '+'. 对于给定的时间数据-2018-06-01 2018-06-01 06:36:40.047883+00:00
,我想删除微秒并去除'+'之后的值。 Most of my dataset contains values like 2018-06-04 11:30:00+00:00
without the microsecond part. 我的大部分数据集都包含2018-06-04 11:30:00+00:00
类的值,没有微秒部分。
How to have a common date time format for all values? 如何为所有值使用通用的日期时间格式?
Let's say you have a mix of different formats that looks like this: 假设您有多种不同的格式,如下所示:
import pandas as pd
df = pd.DataFrame()
df['time'] = ['2018-06-01 06:36:40.047883+00:00', '2018-06-01 06:36:40.047883+00:00', '2018-06-04 11:30:00+00:00', '2018-06-01 06:36:40.047883']
Corresponding output: 对应的输出:
time
0 2018-06-01 06:36:40.047883+00:00
1 2018-06-01 06:36:40.047883+00:00
2 2018-06-04 11:30:00+00:00
3 2018-06-01 06:36:40.047883
You wish to get to a common format by removing microseconds and anything after +. 您希望通过删除微秒和+之后的所有内容来获得通用格式。 In short, you want something that is in YMD HMS format. 简而言之,您需要YMD HMS格式的内容。
Currently, let me assume that your column is in string format. 当前,让我假设您的列是字符串格式。 So, we now convert this to a datetime format and then replace the microseconds part with 0 and get rid of it. 因此,我们现在将其转换为日期时间格式,然后将微秒部分替换为0并摆脱它。
df['time'] = pd.to_datetime(df['time'])
df['time'] = df['time'].apply(lambda x: x.replace(microsecond = 0))
Output: 输出:
time
0 2018-06-01 06:36:40
1 2018-06-01 06:36:40
2 2018-06-04 11:30:00
3 2018-06-01 06:36:40
Another way to achieve that is by using str.split: 实现此目标的另一种方法是使用str.split:
t = "2018-06-04 11:30:00+00:00"
t.split('+')[0]
I'm answering your question with an assumption that the type of the data is a string . 我以数据类型为字符串的假设回答您的问题。
If you are facing problem in handling in different formats like "2018-06-01 06:36:40.047883+00:00"
and "2018-06-04 11:30:00+00:00"
you can use split()
. 如果您在处理不同格式(例如"2018-06-01 06:36:40.047883+00:00"
和"2018-06-04 11:30:00+00:00"
"2018-06-01 06:36:40.047883+00:00"
"2018-06-04 11:30:00+00:00"
遇到问题,则可以使用split()
。 Learn more about split()
at here 在此处了解有关split()
更多信息
str_data_time.split("+")[0].split(".")[0]
for str_data_time in ["2018-06-01 06:36:40.047883+00:00", "2018-06-04 11:30:00+00:00"]:
output = str_data_time.split("+")[0].split(".")[0]
print(output)
The output of the above script is, 上面脚本的输出是,
2018-06-01 06:36:40
2018-06-04 11:30:00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.