потеря точности плавающего числа в данных типа `datetime` при преобразовании типа float64 в данные типа float32 - PullRequest
0 голосов
/ 20 сентября 2019

Я нахожу потерю точности с плавающей точкой в ​​данных типа datetime при преобразовании типа float64 в данные типа float32 следующим образом:

rematch_train_data['time'] = pd.to_datetime(rematch_train_data['nginxtime'], unit = 'ms')
rematch_train_data['sec'] = rematch_train_data['time'].dt.second
rematch_train_data[['sec', time', 'nginxtime']].astype(str).sample()

        sec time                    nginxtime
5752    36  2019-08-03 22:48:36.000 1564872516000.0
3345490 28  2019-08-05 02:34:28.034 1564972468034.0
1337395 27  2019-08-09 06:01:27.476 1565330487476.0
1196978 57  2019-08-03 16:59:57.124 1564851597124.0
1371963 43  2019-08-08 17:22:43.719 1565284963719.0
def reduce_mem_usage(df, verbose=True):
    numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
    start_mem = df.memory_usage().sum() / 1024**2    
    for col in df.columns:
        col_type = df[col].dtypes
        if col_type in numerics:
            c_min = df[col].min()
            c_max = df[col].max()
            if str(col_type)[:3] == 'int':
                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:
                    df[col] = df[col].astype(np.int8)
                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:
                    df[col] = df[col].astype(np.int16)
                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:
                    df[col] = df[col].astype(np.int32)
                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:
                    df[col] = df[col].astype(np.int64)  
            else:
                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:
                    df[col] = df[col].astype(np.float16)
                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:
                    df[col] = df[col].astype(np.float32)
                else:
                    df[col] = df[col].astype(np.float64)    
    end_mem = df.memory_usage().sum() / 1024**2
    if verbose: print('Mem. usage decreased to {:5.2f} Mb ({:.1f}% reduction)'.format(end_mem, 100 * (start_mem - end_mem) / start_mem))
    return df
rematch_train_data = reduce_mem_usage(rematch_train_data)
rematch_train_data['time'] = pd.to_datetime(rematch_train_data['nginxtime'], unit = 'ms')
rematch_train_data['sec'] = rematch_train_data['time'].dt.second
rematch_train_data[['sec', 'time', 'nginxtime']].astype(str).sample(5)

# 05.632 is obviously not equal to the ms part in nginxtime column
        sec time                    nginxtime
1255421 5   2019-08-04 06:08:05.632 1564898900000.0
3159384 55  2019-08-04 13:35:55.392 1564925800000.0
1596070 3   2019-08-04 11:51:03.936 1564919500000.0
1481053 47  2019-08-08 13:27:47.968 1565270900000.0
2882284 10  2019-08-06 13:59:10.080 1565100000000.0

И нахожу часть ms вtime функция не равна nginxtime функция, что странно после преобразования типа данных.
Я знаю потеря точности между float32 и float64 и, конечно, десятичная часть здесьвсе ноль и не показывает потери точности в столбце nginxtime.Но я хочу знать, почему потеря точности происходит только после преобразования во временной столбец.Я искал некоторые соответствующие проблемы, но не нашел, что может объяснить это хорошо.
Если бы не разум, кто-нибудь мог бы помочь мне и дать мне несколько советов.
Спасибо заранее.

...