如何用大熊猫将时间戳分隔成单独的列?

时间:2017-03-09 19:10:17

标签: python pandas

我想将我的时间戳分成不同的列,因为我想按年对数据进行排序。我的时间戳看起来像这样:

  12/08/2011 11:04:13 AM

数据样本:

BusinessName     DBAName    LegalOwner  NameLast    NameFirst   ISSDTTM
NEW GARDEN RESTAURANT ZHENG JIA XIANG   12/8/11 11:04
Estragon        CITY OF BOSTON  De Haro Julio   1/18/12 17:01
TAIWAN CAFE     Yan-Fang Zhang Pres.    8/3/12 14:54
Earl of Sandwich Earl of Sandwich (Boston) L L C    1/7/13 9:08
TAIWAN CAFE         Yan-Fang Zhang Pres.    8/3/12 14:54

我试过用这个:

df = pd.read_csv("Food_Establishment_Inspections.csv", low_memory=False, index_col='datetime', 
parse_dates={'year':[0]}, 
date_parser=lambda x:pd.datetime.strptime(x,'%m/%d/%Y %I:%M:%S %p').strftime('%Y'))

但是我收到一个错误,我在下面显示:

   TypeError                                 Traceback (most recent call last)
//anaconda/lib/python3.6/site-packages/pandas/io/parsers.py in converter(*date_cols)
   2661                 result = tools.to_datetime(
-> 2662                     date_parser(*date_cols), errors='ignore')
   2663                 if isinstance(result, datetime.datetime):

<ipython-input-60-4bd0710aff18> in <lambda>(x)
      2                  parse_dates={'year':[0]},
----> 3                  date_parser=lambda x:pd.datetime.strptime(x,'%m/%d/%Y %I:%M:%S %p').strftime('%Y'))

TypeError: strptime() argument 1 must be str, not numpy.ndarray

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
//anaconda/lib/python3.6/site-packages/pandas/io/parsers.py in converter(*date_cols)
   2670                                             parser=date_parser,
-> 2671                                             dayfirst=dayfirst),
   2672                         errors='ignore')

pandas/src/inference.pyx in pandas.lib.try_parse_dates (pandas/lib.c:61053)()

pandas/src/inference.pyx in pandas.lib.try_parse_dates (pandas/lib.c:60951)()

<ipython-input-60-4bd0710aff18> in <lambda>(x)
      2                  parse_dates={'year':[0]},
----> 3                  date_parser=lambda x:pd.datetime.strptime(x,'%m/%d/%Y %I:%M:%S %p').strftime('%Y'))

//anaconda/lib/python3.6/_strptime.py in _strptime_datetime(cls, data_string, format)
    564     format string."""
--> 565     tt, fraction = _strptime(data_string, format)
    566     tzname, gmtoff = tt[-2:]

//anaconda/lib/python3.6/_strptime.py in _strptime(data_string, format)
    361         raise ValueError("time data %r does not match format %r" %
--> 362                          (data_string, format))
    363     if len(data_string) != found.end():

ValueError: time data 'NEW GARDEN RESTAURANT' does not match format '%m/%d/%Y %I:%M:%S %p'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-60-4bd0710aff18> in <module>()
      1 df = pd.read_csv("Food_Establishment_Inspections.csv", low_memory=False, index_col='datetime', 
      2                  parse_dates={'year':[0]},
----> 3                  date_parser=lambda x:pd.datetime.strptime(x,'%m/%d/%Y %I:%M:%S %p').strftime('%Y'))

//anaconda/lib/python3.6/site-packages/pandas/io/parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skipfooter, skip_footer, doublequote, delim_whitespace, as_recarray, compact_ints, use_unsigned, low_memory, buffer_lines, memory_map, float_precision)
    644                     skip_blank_lines=skip_blank_lines)
    645 
--> 646         return _read(filepath_or_buffer, kwds)
    647 
    648     parser_f.__name__ = name

//anaconda/lib/python3.6/site-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
    399         return parser
    400 
--> 401     data = parser.read()
    402     parser.close()
    403     return data

//anaconda/lib/python3.6/site-packages/pandas/io/parsers.py in read(self, nrows)
    937                 raise ValueError('skipfooter not supported for iteration')
    938 
--> 939         ret = self._engine.read(nrows)
    940 
    941         if self.options.get('as_recarray'):

//anaconda/lib/python3.6/site-packages/pandas/io/parsers.py in read(self, nrows)
   1583             data = dict((k, v) for k, (i, v) in zip(names, data))
   1584 
-> 1585             names, data = self._do_date_conversions(names, data)
   1586             index, names = self._make_index(data, alldata, names)
   1587 

//anaconda/lib/python3.6/site-packages/pandas/io/parsers.py in _do_date_conversions(self, names, data)
   1362             data, names = _process_date_conversion(
   1363                 data, self._date_conv, self.parse_dates, self.index_col,
-> 1364                 self.index_names, names, keep_date_col=self.keep_date_col)
   1365 
   1366         return names, data

//anaconda/lib/python3.6/site-packages/pandas/io/parsers.py in _process_date_conversion(data_dict, converter, parse_spec, index_col, index_names, columns, keep_date_col)
   2724 
   2725             _, col, old_names = _try_convert_dates(converter, colspec,
-> 2726                                                    data_dict, orig_names)
   2727 
   2728             new_data[new_name] = col

//anaconda/lib/python3.6/site-packages/pandas/io/parsers.py in _try_convert_dates(parser, colspec, data_dict, columns)
   2756     to_parse = [data_dict[c] for c in colnames if c in data_dict]
   2757 
-> 2758     new_col = parser(*to_parse)
   2759     return new_name, new_col, colnames
   2760 

//anaconda/lib/python3.6/site-packages/pandas/io/parsers.py in converter(*date_cols)
   2672                         errors='ignore')
   2673                 except Exception:
-> 2674                     return generic_parser(date_parser, *date_cols)
   2675 
   2676     return converter

//anaconda/lib/python3.6/site-packages/pandas/io/date_converters.py in generic_parser(parse_func, *cols)
     36     for i in range(N):
     37         args = [c[i] for c in cols]
---> 38         results[i] = parse_func(*args)
     39 
     40     return results

<ipython-input-60-4bd0710aff18> in <lambda>(x)
      1 df = pd.read_csv("Food_Establishment_Inspections.csv", low_memory=False, index_col='datetime', 
      2                  parse_dates={'year':[0]},
----> 3                  date_parser=lambda x:pd.datetime.strptime(x,'%m/%d/%Y %I:%M:%S %p').strftime('%Y'))

//anaconda/lib/python3.6/_strptime.py in _strptime_datetime(cls, data_string, format)
    563     """Return a class cls instance based on the input string and the
    564     format string."""
--> 565     tt, fraction = _strptime(data_string, format)
    566     tzname, gmtoff = tt[-2:]
    567     args = tt[:6] + (fraction,)

//anaconda/lib/python3.6/_strptime.py in _strptime(data_string, format)
    360     if not found:
    361         raise ValueError("time data %r does not match format %r" %
--> 362                          (data_string, format))
    363     if len(data_string) != found.end():
    364         raise ValueError("unconverted data remains: %s" %

ValueError: time data 'NEW GARDEN RESTAURANT' does not match format '%m/%d/%Y %I:%M:%S %p'

2 个答案:

答案 0 :(得分:0)

pandas.read_csv()函数有一个名为parse_dates的关键字参数。

您可以在此处详细了解:datetime dtypes in pandas read_csv

答案 1 :(得分:0)

首先,你要传递给strptime的格式,%Y%m%d%H&#39;并不匹配您提供的日期/时间字符串作为示例。另外,你的日期时间只在一列,为什么你在parse_dates参数中传递[1,2,3,4]?

假设您的CSV内容位于名为foo.csv(无标题)的文件中并包含:

12/08/2011 11:04:13 AM,35
12/08/2011 11:04:13 AM,82
12/08/2011 11:04:13 AM,11
12/08/2011 11:04:13 AM,21
12/08/2011 11:04:13 AM,91
12/08/2011 11:04:13 AM,44

以下内容应该有效:

df = pd.read_csv('foo.csv',  parse_dates={'datetime':[0]}, date_parser=lambda x:pd.datetime.strptime(x,'%m/%d/%Y %I:%M:%S %p'))

如果您只想保留年份:

df = pd.read_csv('foo.csv',  parse_dates={'year':[0]}, date_parser=lambda x:pd.datetime.strptime(x,'%m/%d/%Y %I:%M:%S %p').strftime('%Y'))