Mainland
Mainland

Reputation: 4564

Python setting the extracted Year-month in the Datetime format

I have a df with index in datetime format. I want to extract year-month of each sample. This I will use later for coloring the samples in scatter plots.

My code:

df.index = 
DatetimeIndex(['2019-10-01 08:16:57', '2019-10-01 08:43:54',
               '2019-10-01 08:45:24', '2019-10-01 08:49:42',
               '2019-10-01 09:01:04', '2019-10-01 09:07:04',
               '2019-10-01 09:07:40', '2019-10-01 09:07:46',
               '2019-10-01 09:14:09', '2019-10-01 09:14:57',
               '2020-11-13 14:22:58', '2020-11-13 14:23:01',
               '2020-11-13 14:31:40', '2020-11-13 14:31:58',
               '2020-11-13 14:32:49', '2020-11-13 14:33:13',
               '2020-11-13 14:36:16', '2020-11-13 14:40:03',
               '2020-11-13 15:01:24', '2020-11-14 09:57:21'],
              dtype='datetime64[ns]', name='timestamp', length=104676, freq=None) 
df['year-month'] = df.index.strftime('%Y-%m')
print(df['year-month'])
Index(['2019-10', '2019-10', '2019-10', '2019-10', '2019-10', '2019-10',
       '2019-10', '2019-10', '2019-10', '2019-10',
       ...
       '2020-11', '2020-11', '2020-11', '2020-11', '2020-11', '2020-11',
       '2020-11', '2020-11', '2020-11', '2020-11'],
      dtype='object', name='timestamp', length=104676)

Extracted year-month is in object type, not datetime type as df.index. I want to plot a scatter plot for the df. I want to color the sample based on the year and month it belongs. Now when I set plt.scatter(x,y,c=df['year-month']) it raises an error. So, converting it to timestamp format could solve the scatter plot issue.

Upvotes: 1

Views: 166

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

You can do:

df['year-month'] = df.index.to_period('M')

Upvotes: 1

Related Questions