澍梵. 发表于 2020-3-21 19:57:55

错误显示:unsupported operand type(s) for -: 'str' and 'str' 如何解决呀!

本帖最后由 澍梵. 于 2020-4-5 20:05 编辑

源文件数据:

    id               time               jd               wd
012018-12-27 10:46:24118.819686032.3586430
112018-12-27 10:48:57118.825751032.3325110
212018-12-27 10:49:02118.825423032.3306620
312018-12-27 10:49:07118.827557032.3325600
412018-12-27 10:54:40118.777099032.2727000


源代码:

from math import sin, cos, asin, sqrt
import numpy as np
import pandas as pd
import datetime

data = pd.read_excel('userdata_1.xlsx')

def GetDistance(jdA, wdA, jdB, wdB):
    #    jdA,wdA,jdB,wdB = map(radians, ) # 经纬度转换成弧度
    dlon = jdB - jdA
    dlat = wdB - wdA
    a = sin(dlat / 2) ** 2 + cos(wdA) * cos(wdB) * sin(dlon / 2) ** 2
    distance = 2 * asin(sqrt(a)) * 6371 * 1000# 地球平均半径,6371km
    distance = round(distance / 1000, 3)
    return distance

# 借助多点金纬度求其中心点
def ComputeMeanCoord(data, start, end):
    jd = data.loc
    wd = data.loc
    x = np.mean(jd)
    y = np.mean(wd)
    return (x, y)


# 获取停留点的函数,,data为dataframe对象,距离门限单位为米,时间门限单位为秒


if __name__ == "__main__":
    data = pd.read_excel('userdata_1.xlsx')
    #   distThreh,timeThreh=map(eval,input("距离跨度(用空格隔开),时间距(以分钟为单位):").split())
    #   StayPoint_Detection(data,distThreh,timeThreh)
    SP = StayPoint_Detection(data,15,60)
    print('停留点数目=', len(SP))
    print(SP)


错误显示:


wp231957 发表于 2020-3-21 19:59:38

42行str和str不能做减法运算

澍梵. 发表于 2020-3-21 20:00:04

希望会的可以帮忙帮我解决下:这里是时间相减: Time = TraTime - TraTime
                                                   时间格式为:2018-12-27 10:46:24
感谢各位的帮助

sunrise085 发表于 2020-3-21 20:09:08

datetime中有个函数可以将字符串转为可读类型的date
datetime.datetime.strptime('2017-3-22 15:25:32','%Y-%m-%d %H:%M:%S')
可以将你所读到的字符串转为date,然后就能用date的各种函数了

澍梵. 发表于 2020-3-21 20:10:16

wp231957 发表于 2020-3-21 19:59
42行str和str不能做减法运算

我知道,42行是时间相减时间格式:2018-12-27 10:46:24 该怎么修改

澍梵. 发表于 2020-3-21 20:15:13

sunrise085 发表于 2020-3-21 20:09
datetime中有个函数可以将字符串转为可读类型的date
datetime.datetime.strptime('2017-3-22 15:25:32','% ...

谢谢,运行出来了,非常感谢
页: [1]
查看完整版本: 错误显示:unsupported operand type(s) for -: 'str' and 'str' 如何解决呀!