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_work\bbb_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)






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值