我是靠谱客的博主 昏睡玫瑰,这篇文章主要介绍py,时间模块,现在分享给大家,希望可以做个参考。

目录:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# -*- 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内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部