python-3.x - 当时间随日期更改时如何添加额外的时间列
问题描述
这个问题与这个问题有关:How to add extra time into the time column with data using python
在这里,我有一个包含日期、时间和一个输入列的数据集。所以这里我的时间专栏不好。所以我想在那个时间列中给出时间范围。所以在这里我首先将开始时间转换为 0 并将整个时间列转换为分钟。
然后接下来我想给出时间范围0,60,120....
意思是我预期的输出是:
第一次转换
date time time convert
10/3/2018 6:15:00 0
10/3/2018 6:45:00 30
10/3/2018 7:45:00 90
10/3/2018 9:00:00 165
10/3/2018 9:25:00 190
10/3/2018 9:30:00 195
10/3/2018 11:00:00 285
10/3/2018 11:30:00 315
10/3/2018 13:30:00 435
10/3/2018 13:50:00 455
10/3/2018 15:00:00 525
10/3/2018 15:25:00 550
10/3/2018 16:25:00 610
10/3/2018 18:00:00 705
10/3/2018 19:00:00 765
10/3/2018 19:30:00 795
10/3/2018 20:00:00 825
10/3/2018 22:05:00 950
10/3/2018 22:15:00 960
10/3/2018 23:40:00 1045
10/4/2018 6:58:00 0
10/4/2018 13:00:00 2
10/4/2018 16:00:00 47
10/4/2018 17:00:00 152
预期产出
expected time
0.0
30.0
60
90.0
120
165.0
180
190.0
195.0
240
285.0
300
315.0
360
420
435
455
480
525
540
550
:
:
:
:
0 new date ,start time=0
2
47
60
120
152
180
所以这里每个新的一天开始时间应该是 0 然后应该添加 60 分钟 60 分钟到时间。
在这里我尝试了一个代码,它给了我这个错误“无法从重复的轴重新索引”
代码 :
data['date']= pd.to_datetime(data['date'] + " " + data['time'],
format='%d/%m/%Y %H:%M:%S', dayfirst=True)
lastday = data.loc[0, 'date']
def convert_time(x):
global lastday
if x.date() == lastday.date():
tm = x - lastday
return tm.total_seconds()/60
else:
lastday = x
return 0
data['time'] = data['date'].apply(convert_time).values
data = data.reset_index()
arr = np.arange(0, int(data['time'].max()), 60)
union = np.union1d(data['time'], arr)
data = data.set_index('time').reindex(union, fill_value=0).reset_index()
错误 :
ValueError Traceback (most recent call last)
<ipython-input-27-bdb960d8efe1> in <module>()
26 arr = np.arange(0, int(data['time'].max()), 60)
27 union = np.union1d(data['time'], arr)
---> 28 data = data.set_index('time').reindex(union, fill_value=0).reset_index()
~\Anaconda3\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
184 @wraps(func)
185 def wrapper(*args, **kwargs):
--> 186 return func(*args, **kwargs)
187
188 if not PY2:
~\Anaconda3\lib\site-packages\pandas\core\frame.py in reindex(self, *args, **kwargs)
3561 kwargs.pop('axis', None)
3562 kwargs.pop('labels', None)
-> 3563 return super(DataFrame, self).reindex(**kwargs)
3564
3565 @Appender(_shared_docs['reindex_axis'] % _shared_doc_kwargs)
~\Anaconda3\lib\site-packages\pandas\core\generic.py in reindex(self, *args, **kwargs)
3683 # perform the reindex on the axes
3684 return self._reindex_axes(axes, level, limit, tolerance, method,
-> 3685 fill_value, copy).__finalize__(self)
3686
3687 def _reindex_axes(self, axes, level, limit, tolerance, method, fill_value,
~\Anaconda3\lib\site-packages\pandas\core\frame.py in _reindex_axes(self, axes, level, limit, tolerance, method, fill_value, copy)
3496 if index is not None:
3497 frame = frame._reindex_index(index, method, copy, level,
-> 3498 fill_value, limit, tolerance)
3499
3500 return frame
~\Anaconda3\lib\site-packages\pandas\core\frame.py in _reindex_index(self, new_index, method, copy, level, fill_value, limit, tolerance)
3507 return self._reindex_with_indexers({0: [new_index, indexer]},
3508 copy=copy, fill_value=fill_value,
-> 3509 allow_dups=False)
3510
3511 def _reindex_columns(self, new_columns, method, copy, level,
~\Anaconda3\lib\site-packages\pandas\core\generic.py in _reindex_with_indexers(self, reindexers, fill_value, copy, allow_dups)
3804 fill_value=fill_value,
3805 allow_dups=allow_dups,
-> 3806 copy=copy)
3807
3808 if copy and new_data is self._data:
~\Anaconda3\lib\site-packages\pandas\core\internals.py in reindex_indexer(self, new_axis, indexer, axis, fill_value, allow_dups, copy)
4412 # some axes don't allow reindexing with dups
4413 if not allow_dups:
-> 4414 self.axes[axis]._can_reindex(indexer)
4415
4416 if axis >= self.ndim:
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in _can_reindex(self, indexer)
3557 # trying to reindex on an axis with duplicates
3558 if not self.is_unique and len(indexer):
-> 3559 raise ValueError("cannot reindex from a duplicate axis")
3560
3561 def reindex(self, target, method=None, level=None, limit=None,
ValueError: cannot reindex from a duplicate axis
我的 csv 的子集:
date time X3
10/3/2018 6:15:00 7
10/3/2018 6:45:00 5
10/3/2018 7:45:00 7
10/3/2018 9:00:00 7
10/3/2018 9:25:00 7
10/3/2018 9:30:00 5
10/3/2018 11:00:00 7
10/3/2018 11:30:00 7
10/3/2018 13:30:00 7
10/3/2018 13:50:00 5
10/3/2018 15:00:00 7
10/3/2018 15:25:00 7
10/3/2018 16:25:00 7
10/3/2018 18:00:00 7
10/3/2018 19:00:00 5
我的 csv : 在此处输入链接描述
解决方案
推荐阅读
- android - 为什么 GridLayoutManager 从下到上,从右到左填充项目?
- json - 加载时更改 JSON 文件的编码
- json - 将 Azure Cosmos DB 中存储的实体上的“扩展”元数据存储为 JSON 文档
- typescript - ERROR] 运行子进程 ionic-app-scripts 时发生错误
- python - 多个 JSON 根元素 - Twitter API 流
- powershell - powershell 可以从外部 C# dll 调用子类吗
- magento - Magento CE 2.3.0 - 网站图标和徽标上传问题
- common-lisp - 使用 LISP 的 3 和 5 的倍数之和
- laravel - phpmyadmin 无法登录 laravel valet 中的 MySQL 服务器
- c++ - 递归计算字符串中的表达式