import time
from datetime import datetime,date
# 方法一,利用python内置转换
tmp_time = date(excel['iyear'][0], excel['imonth'][0], excel['iday'][0]) # 生成datetime格式的时间
time.mktime(tmp_time.timetuple()) # 转化为时间戳
883584000.0
result = []
# 方法二,转换位pd.Timestamp
for i in range(excel.shape[0]):
# print(excel['iyear'][i], excel['imonth'][i], excel['iday'][i])
if excel['iday'][i]==0:
excel['iday'][i] = 1
result.append(pd.Timestamp(datetime(excel['iyear'][0], excel['imonth'][0], excel['iday'][0])))
result = pd.Series(result) # 存储所有的起始时间
result.head()
<ipython-input-6-a8da161efb8a>:6: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
excel['iday'][i] = 1
0 1998-01-01
1 1998-01-01
2 1998-01-01
3 1998-01-01
4 1998-01-01
dtype: datetime64[ns]
end = []
for i in range(excel.shape[0]):
if pd.notnull(excel['resolution'][i]): # 相反的是isnull(time)
excel['resolution'][i]=excel['resolution'][i]-result[i]
<ipython-input-7-4bb3bc5e54ae>:4: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
excel['resolution'][i]=excel['resolution'][i]-result[i]
/home/fxz/anaconda3/envs/notebook/lib/python3.8/site-packages/pandas/core/indexing.py:670: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
iloc._setitem_with_indexer(indexer, value)
# help(excel['resolution'][828])
for i in range(excel.shape[0]):
if pd.notnull(excel['resolution'][i]):
if excel['resolution'][i].value < 0: # 设置timedelta为负的值为NaT
excel['resolution'][i] = pd.NaT
<ipython-input-8-bcbd64a0a015>:5: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
excel['resolution'][i] = pd.NaT
result = pd.DataFrame(excel[['nkill','nwound','propextent','propvalue','resolution']])
# resolution 表示持续时间,如果当天解决,设置为86400/2
for i in range(result.shape[0]):
if pd.notnull(result['resolution'][i]):
result.iloc[i,4] = result.iloc[i,4].value # 将持续时间,换成时间戳
else:
result.iloc[i,4] = 43200