简体   繁体   English

float()参数必须是字符串或数字,而不是'Timestamp'

[英]float() argument must be a string or a number, not 'Timestamp'

I can't make scilearn work with a datetime series. 我不能让scilearn使用日期时间系列。

found this post but did not help me = Pandas : TypeError: float() argument must be a string or a number 发现这篇文章但没有帮助我= Pandas:TypeError:float()参数必须是字符串或数字

the csv file has 2 date columns with a date, dates are in the following format: 2017-07-21 06:19:53 (string) csv文件有2个日期列和日期,日期格式如下:2017-07-21 06:19:53(string)

i converted the string to an datetime64[ns], so the date became a long value and i could do calculations on it. 我将字符串转换为datetime64 [ns],因此日期变为长值,我可以对其进行计算。 scilearn refuses this type and gives the error float() argument must be a string or a number, not 'Timestamp' scilearn拒绝这种类型并给出错误float()参数必须是字符串或数字,而不是'Timestamp'

also tried with pandas.to_datetime() no luck. 也尝试过pandas.to_datetime()没有运气。

the model i use in scilearn is the KMeans clustering model. 我在scilearn中使用的模型是KMeans聚类模型。 when printing the dtypes this is the result: 打印dtypes时,结果如下:

ip                      int64
date           datetime64[ns]
succesFlag              int64
app                     int64
enddate        datetime64[ns]
user_userid             int64
dtype: object

Here is my code: 这是我的代码:

def getDataframe():
    df = pd.read_csv(filename)
    df['date']=df['date'].astype('datetime64[ns]',inplace=True)
    df['enddate']=df['enddate'].astype('datetime64[ns]',inplace=True)
    df['app']=df['app'].replace({
            "Azure": 0 ,
            "Peoplesoft":1,
            "Office":2 ,
            "DevOps":3 ,
            "Optima":4 ,
            "Ada-Tech": 5 
         },inplace=True)    
    df['ip']=df['ip'].apply(lambda x: int(ip4.ip_address(x))).to_frame('ip')
    print(df.dtypes)
    return df

the expectation was that KMeans clustering model would work with numerical values as i converted them but it did not. 期望是KMeans聚类模型可以使用数值,因为我转换它们但它没有。

what did i wrong ? 我错了什么?

I suggest change your solution - a but simplify also: 我建议改变你的解决方案 - 但也要简化:

  • add parameter parse_dates for converting columns to datetimes and then to numeric unix datetimes 添加参数parse_dates以将列转换为日期时间,然后再转换为数字unix日期时间
  • for converting remove inplace=True or use faster map - it also create NaNs for non matched values, so output is numeric too 用于转换remove inplace=True或使用更快的map - 它还为非匹配值创建NaN,因此输出也是数字

def getDataframe():
    df = pd.read_csv(filename, parse_dates=['date','enddate'])
    df[['date','enddate']] = df[['date','enddate']].astype(np.int64) // 10**9

    df['app']=df['app'].map({
            "Azure": 0 ,
            "Peoplesoft":1,
            "Office":2 ,
            "DevOps":3 ,
            "Optima":4 ,
            "Ada-Tech": 5 
         })    
    df['ip']=df['ip'].apply(lambda x: int(ip4.ip_address(x))).to_frame('ip')
    print(df.dtypes)
    return df

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:float()参数必须是字符串或数字,而不是“ Timestamp” - TypeError: float() argument must be a string or a number, not 'Timestamp' 类型错误:float() 参数必须是字符串或数字,而不是“时间戳” - TypeError: float() argument must be a string or a number, not 'Timestamp'' Numpy:TypeError:float() 参数必须是字符串或数字,而不是“时间戳” - Numpy: TypeError: float() argument must be a string or a number, not 'Timestamp' OneHotEncoding 错误:类型错误:float() 参数必须是字符串或数字,而不是“时间戳” - OneHotEncoding Error:TypeError: float() argument must be a string or a number, not 'Timestamp' 如何解决“ float()参数必须是字符串或数字,而不是'Timestamp'”错误? - How to fix “float() argument must be a string or a number, not 'Timestamp' ” error? float() 参数必须是字符串或数字,而不是“时间戳”:python pandas - float() argument must be a string or a number, not 'Timestamp': python pandas float参数必须是字符串或数字 - float argument must be a string or a number float() 参数必须是字符串或数字,而不是 'Float' - float() argument must be a string or a number, not 'Float' Pandas:TypeError:float() 参数必须是字符串或数字 - Pandas : TypeError: float() argument must be a string or a number Python:float() 参数必须是字符串或数字,而不是“期间” - Python: float() argument must be a string or a number, not 'Period'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM