How to normalize the date columns of one panda data frame (ValueError: could not convert string to float: '17-Aug-20 00:00:00')

Viewed 190

I have open pandas CSV file by Time columns like shown below: enter image description here

So I am trying to normalize the data (df variable) by below command:

import pandas as pd
from sklearn import preprocessing

import numpy as np
from sklearn.preprocessing import MinMaxScaler
import time


minmax = MinMaxScaler().fit(df.iloc[:].values.reshape((-1,1)))
df_log = MinMaxScaler().fit_transform(df.iloc[:].astype('float32'))

df.head()

or

df = pd.DataFrame(df.astype('float64'), columns=['Time'])

# specify your desired range (-1, 1)
scaler = MinMaxScaler(feature_range=(-1, 1))
scaled = scaler.fit_transform(df.values)
print(scaled)

But I get this error by running the two above code block:

~/anaconda3/lib/python3.8/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
     81 
     82     """
---> 83     return array(a, dtype, copy=False, order=order)
     84 
     85 

ValueError: could not convert string to float: '17-Aug-20 00:00:00'

So if possible, asked here to find out how to normalize the date columns of one panda data frame.

thanks.

1 Answers

You could try this:

import pandas as pd
from sklearn.preprocessing import MinMaxScaler

df = pd.DataFrame(
    {
        "time": [
            "17-Aug-20 00:00:00",
            "17-Aug-20 00:01:00",
            "17-Aug-20 00:02:00",
            "17-Aug-20 00:03:00",
            "17-Aug-20 00:04:00",
        ],
    }
)

# Convert to datetime type
df["time"] = pd.to_datetime(df["time"])

# Convert to Unix timestamp seconds
df["time"] = (df["time"] - pd.Timestamp("1970-01-01")) // pd.Timedelta("1s")

# Scale values
scaler = MinMaxScaler(feature_range=(-1, 1))
scaled = scaler.fit_transform(df["time"].values.reshape(-1, 1))

print(scaled)
# Outputs
[[-1. ] 
 [-0.5] 
 [ 0. ] 
 [ 0.5] 
 [ 1. ]]
Related