我是靠谱客的博主 昏睡玫瑰,最近开发中收集的这篇文章主要介绍py,时间模块,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

目录:

# -*- coding: utf-8 -*-
"""
Created on Wed Dec 15 20:51:04 2021

@author: 62669
"""
# In[1]:
import pandas as pd
import datetime
import re
import numpy as np
from presto_cli import presto_client
import win32com.client as win32
import time as tm
from datetime import datetime
import datetime
#处理工作中遇到的
# In[1]:

#字段类型转换问题
wo['放款时效'] = wo['放款时效'].astype('float64')
wo['give_time'] = pd.to_datetime(wo['give_time'],format='%Y-%m-%d',errors='coerce')

#日期格式处理问题
today = datetime.date.today()
current_month_first= today.replace(day=1)
last_month_last = current_month_first - datetime.timedelta(days=1)
last_month_first = last_month_last.replace(day=1)
print(last_month_first.strftime("%Y-%m"))

def make_week(dt):
    if dt.day <= 7 :
        rn = str(dt.year) + str(dt.month) + '第1周'
    elif dt.day >7 and dt.day<=14:
        rn = str(dt.year) + str(dt.month) + '第2周'
    elif dt.day >14 and dt.day<=21:
        rn = str(dt.year) + str(dt.month) + '第3周'
    else :
        rn = str(dt.year) + str(dt.month) + '第4周'
    return rn
wo['give_time_day'] = wo['give_time'].map(lambda x:x.date())
wo['give_time_month'] =wo['give_time'].map(lambda x:x.date().replace(day=1))  
#wo['give_time'] =xs[i].map(lambda x:x.date()).map(make_week)

#groupby问题
wo["fk_shixiao_0"] = wo['放款时效'].apply( lambda x: 1 if x<=0.5  else 0 )
wo3 = wo.groupby(['give_time_month'])['fk_shixiao_0'].mean().reset_index()
tmp1 = xs.groupby([col_2])['shenhe_shixiao'].quantile(0.5).reset_index()

#merge问题
fk = pd.merge(fk,wo4,how='left',left_on=['give_time_month'],right_on=['give_time_month'])  


import numpy as np
import pandas as pd
import datetime

today = datetime.date.today()
type(today)
a=str(today)

now = datetime.datetime.now()
print(type(now))

t1 = datetime.datetime(2019,10,22)

t2 = datetime.datetime(2019,10,22,11,45,36)

t2-t1

print( t2-t1 )

tx = datetime.timedelta(100)

print(t2-tx)

from dateutil.parser import parse

date ='20170501'
date2 ='21/12/2019'

print(parse(date),type(parse(date)))

########pandas timestamp

date2 ='21/12/2019'
t1 = pd.Timestamp(date2)

t2 = pd.to_datetime(date2)

date3 =['21/12/2019','22/12/2019','23/12/2019','aa']
a = pd.to_datetime(date3,errors='ignore')
print(pd.to_datetime(date3,errors='ignore'))
print(pd.to_datetime(date3,errors='coerce'))
print(pd.to_datetime(date3,errors='ignore'))


ex = pd.date_range('2000-1-1', periods=1000, freq='M')
print(ex)

ex.date

#6字符串转换成datetime格式: strptime
df_data1  = pd.DataFrame(columns=['date','values'])
df_data1['date'] = ['2019-01-01','2019-01-02','2019-01-03','2019-01-04','2019-01-05']
df_data1['values'] = np.random.randn(5)

type(df_data1['date'][2])

df_data1['date1'] = df_data1['date'].map(lambda x:datetime.datetime.strptime(x,'%Y-%m-%d'))
df_data1
df_data1.info()

#6datetime变回string格式: strftime
df_data  = pd.DataFrame(columns=['date','values'])
df_data['date'] = pd.date_range('2019/01/01',periods=5)
df_data['values'] = np.random.randn(5)
df_data

df_data.info()

df_data['date1'] = df_data['date'].apply(lambda x:x.strftime('%Y-%m')) #datetime格式转成str




# -*- coding: utf-8 -*-
"""
Created on Thu Feb 11 11:55:42 2021

@author: 62669
"""
import pandas as pd
import numpy as np

pt = r'F:a_houdashi_workbbb_python学习pandas操作清洗1.xlsx'
t = pd.read_excel(pt,skiprows=1)

t.isnull()
t['分数'].isnull()

t.head()

t.dropna(axis="columns",how="all",inplace=True)
t.dropna(axis="index",how="all",inplace=True)

t.fillna({'分数':0})
t.loc[:,'分数'] = t['分数'].fillna(0)
t['分数'] = t['分数'].fillna(0)

t.loc[:,'姓名'] = t['姓名'].fillna(method='ffill')
t['姓名'] = t['姓名'].fillna(method='ffill')

#设置筛选条件
con= t['姓名']=='小明' 

t.loc[con,'新分数']=t['分数']*2
t[con].head()

tt = t[con].copy()
tt['新分数']=tt['分数']*2

#排序
t['分数'].sort_values()

t.sort_values(by=['分数'],ascending=False)
t.sort_values(by=['分数'],ascending=True)
t.sort_values(by=['分数','姓名'],ascending=True)






最后

以上就是昏睡玫瑰为你收集整理的py,时间模块的全部内容,希望文章能够帮你解决py,时间模块所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部