Extracting seconds from a pandas TimeStamp series: 'Series' object has no attribute 'second'

Question:

I am reading a CSV file using pandas, where one of the columns has a TimeStamp value that looks like this:
2022.7.11/11:34:36:372
My end goal is to extract the TimeStamp value as datetime and subsequently get the values of seconds that I want to use in further arithmetic operations.

So far the code I have:

df = pd.read_csv(location, encoding='utf8', delimiter=';')
s = pd.Series((df['TimeStamp']).values).str.split('/', n=4 ,expand=True)
t1 = pd.to_datetime(s[1] , format='%H:%M:%S:%f')

But when I try to get the seconds value from t1, using t1.second I get an exception that says: > ‘Series’ object has no attribute ‘second’

Where could I be going wrong?

Asked By: agenthost

||

Answers:

After typecasting the object to datetime column , you should be able to access seconds using :

df['TimeStamp'] = pd.to_datetime(df['TimeStamp'], format='%Y.%m.%d/%H:%M:%S:%f')
df['TimeStamp'].dt.second

0    36
Name: TimeStamp, dtype: int64

You can access the microsecond part using

df['TimeStamp'].dt.microsecond
Answered By: Himanshuman

You should use t1.dt.second instead of t1.second

Answered By: Asad Asadzade
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.