当前位置:网站首页>About DataFrame: Processing Time

About DataFrame: Processing Time

2022-08-10 21:32:00 Light of the Earth


The main thing is to summarize the processing time problems that you have encountered,会一直更新的

字符串转时间

Time split in two ways,xx.str.slice() 或者 Extract with regular expressions

提取时间

实例:

re.findall(r'(.*)T.*','2022-05-12T07:56:13')[0]

# '2022-05-12'

方法一:df_data[‘year’] = pd.to_datetime(df_data[‘date’]).dt.year

# 转换为时间类型
df["date"] = pd.to_datetime(df["date"], format='%Y-%m-%d')
# 获取年
df["year"] = pd.to_datetime(df["date"]).dt.year
# 获取月
df["month"] = pd.to_datetime(df["date"]).dt.month
# 获取日
df["day"] = pd.to_datetime(df["date"]).dt.day
# 获取周
df["week"] = pd.to_datetime(df["date"]).dt.week
print(df)
print(df.dtypes)

结果:

        date  year  month  day  week
0 2019-12-09  2019     12    9    50
1 2019-12-02  2019     12    2    49
date     datetime64[ns]
year              int64
month             int64
day               int64
week              int64
dtype: object

方法二:df_data[‘year’] = df_data[‘date’].apply(lambda x:x.strftime(“%Y”))

# 转换为时间
df["date"] = pd.to_datetime(df["date"])
# 获取年月日
df["year-month-day"] = df["date"].apply(lambda x: x.strftime("%Y-%m-%d"))
# 获取年
df["year"] = df["date"].apply(lambda x: x.strftime("%Y"))
# 获取月
df["month"] = df["date"].apply(lambda x: x.strftime("%m"))
# 获取日
df["day"] = df["date"].apply(lambda x: x.strftime("%d"))
# 获取月日
df["month-day"] = df["date"].apply(lambda x: x.strftime("%Y-%m"))
# 获取周
df['week'] = df['date'].apply(lambda x: x.strftime('%W'))
print(df)
print(df.dtypes)
原网站

版权声明
本文为[Light of the Earth]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208102102329706.html