Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Where could I be going wrong?

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading