Python - Круглый столбец CSV до ближайших 30 минут - PullRequest
0 голосов
/ 24 февраля 2019

Мои данные CSV следующие:

Столбцы:

  • CRASH_MONTH (например, "1")
  • CRASH_DAY (например, "1 ")
  • TIMESTR (например," 8:40 ")

Желаемый результат:

Новый столбец с именем" CRASH_DATETIME "сdatetime Объект Python, основанный на соответствующей дате.Год не имеет значения, основная цель - отслеживать аварии по месяцам, дням и часам: минутам, которые должны быть округлены до ближайших 30 минут.

Попробовал следующее, но не получилось:

from datetime import datetime, timedelta

def ceil_dt(month, day, hourWithMinutes, delta):
   hour,minutes = hourWithMinutes.split(':')
   int(month)
   int(day)
   int(hour)
   int(minutes)

   dt = datetime.datetime(month=month, day=day, hour=hour, minute=minutes)
   return dt + (datetime.min - dt) % delta

и

dataInitial['TIME'] = dataInitial.apply(lambda row: ceil_dt(row['CRASH_MONTH'], row['CRASH_DAY'], row['TIMESTR'], '30'))

Но не удалось ( с использованием ноутбука Jupyter ):

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5126)()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item (pandas/_libs/hashtable.c:14010)()

TypeError: an integer is required

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-40-a9ef29fd7eb7> in <module>()
----> 1 dataInitial['TIME'] = dataInitial.apply(lambda row: ceil_dt(row['CRASH_MONTH'], row['CRASH_DAY'], row['TIMESTR'], '30'))

~/anaconda2/envs/tfdeeplearning/lib/python3.5/site-packages/pandas/core/frame.py in apply(self, func, axis, broadcast, raw, reduce, args, **kwds)
   4260                         f, axis,
   4261                         reduce=reduce,
-> 4262                         ignore_failures=ignore_failures)
   4263             else:
   4264                 return self._apply_broadcast(f, axis)

~/anaconda2/envs/tfdeeplearning/lib/python3.5/site-packages/pandas/core/frame.py in _apply_standard(self, func, axis, ignore_failures, reduce)
   4356             try:
   4357                 for i, v in enumerate(series_gen):
-> 4358                     results[i] = func(v)
   4359                     keys.append(v.name)
   4360             except Exception as e:

<ipython-input-40-a9ef29fd7eb7> in <lambda>(row)
----> 1 dataInitial['TIME'] = dataInitial.apply(lambda row: ceil_dt(row['CRASH_MONTH'], row['CRASH_DAY'], row['TIMESTR'], '30'))

~/anaconda2/envs/tfdeeplearning/lib/python3.5/site-packages/pandas/core/series.py in __getitem__(self, key)
    599         key = com._apply_if_callable(key, self)
    600         try:
--> 601             result = self.index.get_value(self, key)
    602 
    603             if not is_scalar(result):

~/anaconda2/envs/tfdeeplearning/lib/python3.5/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   2475         try:
   2476             return self._engine.get_value(s, k,
-> 2477                                           tz=getattr(series.dtype, 'tz', None))
   2478         except KeyError as e1:
   2479             if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value (pandas/_libs/index.c:4404)()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value (pandas/_libs/index.c:4087)()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5210)()

KeyError: ('CRASH_MONTH', 'occurred at index CRASH_DATE')

Есть идеи?

1 Ответ

0 голосов
/ 24 февраля 2019

Ваша функция имеет некоторые незначительные проблемы, связанные с преобразованиями (не сохраненными в переменной), отсутствием года и timedelta.Эта версия функции работает правильно:

from datetime import datetime, timedelta

def ceil_dt(month, day, hourWithMinutes, delta):
    hour,minutes = hourWithMinutes.split(':')
    month = int(month)
    day = int(day)
    hour = int(hour)
    minutes = int(minutes)

    dt = datetime(year = 2019, month=month, day=day, hour=int(hour), minute=int(minutes))

    return dt + (datetime.min - dt) % timedelta(minutes=int(delta))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...