将时间和日期的单独列组合到熊猫中

时间:2017-06-19 06:44:44

标签: python pandas

我有像这样的excel数据

enter image description here

我想使用以下代码

组合日期和时间列
Traceback (most recent call last):
  File "/serving/bazel-bin/tensorflow_serving/example/twitter-sentiment-cnn_saved_model.runfiles/tf_serving/tensorflow_serving/example/twitter-sentiment-cnn_saved_model.py", line 222, in <module>
    embedded_chars = tf.nn.embedding_lookup(W, data_in)
  File "/serving/bazel-bin/tensorflow_serving/example/twitter-sentiment-cnn_saved_model.runfiles/org_tensorflow/tensorflow/python/ops/embedding_ops.py", line 122, in embedding_lookup
    return maybe_normalize(_do_gather(params[0], ids, name=name))
  File "/serving/bazel-bin/tensorflow_serving/example/twitter-sentiment-cnn_saved_model.runfiles/org_tensorflow/tensorflow/python/ops/embedding_ops.py", line 42, in _do_gather
    return array_ops.gather(params, ids, name=name)
  File "/serving/bazel-bin/tensorflow_serving/example/twitter-sentiment-cnn_saved_model.runfiles/org_tensorflow/tensorflow/python/ops/gen_array_ops.py", line 1179, in gather
    validate_indices=validate_indices, name=name)
  File "/serving/bazel-bin/tensorflow_serving/example/twitter-sentiment-cnn_saved_model.runfiles/org_tensorflow/tensorflow/python/framework/op_def_library.py", line 589, in apply_op
    param_name=input_name)
  File "/serving/bazel-bin/tensorflow_serving/example/twitter-sentiment-cnn_saved_model.runfiles/org_tensorflow/tensorflow/python/framework/op_def_library.py", line 60, in _SatisfiesTypeConstraint
    ", ".join(dtypes.as_dtype(x).name for x in allowed_list)))
TypeError: Value passed to parameter 'indices' has DataType string not in list of allowed values: int32, int64

但它会打印出这样的结果。

all links available in inspect element

我希望最后一列的格式如import pandas df = pd.read_excel('selfmade.xlsx') df['new'] = df['Date'].map(str) + df['Time'].map(str) print(df)

我应该在代码中更改哪些内容才能获得所需的结果

1 个答案:

答案 0 :(得分:2)

我认为您需要to_datetimeto_timedelta,还需要astypeTime列转换为string

df['new'] = pd.to_datetime(df['Date']) + pd.to_timedelta(df['Time'].astype(str))

如果dtype列的Date已经datetime

 df['new'] = df['Date'] + pd.to_timedelta(df['Time'].astype(str))