Lee
Lee

Reputation: 57

How to change Datetime formate without converting to string

I have a data frame that I'm trying to create a tsplot using seaborn, the first issue I'm having after converting my DateTime from string to a DateTime object is that the day had been automatically added.

The original data frame looks like this:

zillowvisualtop5.head()
Out[161]: 
        City_State     Year     Price
0     New York, NY  1996-04  169300.0
1  Los Angeles, CA  1996-04  157700.0
2      Houston, TX  1996-04   86500.0
3      Chicago, IL  1996-04  114000.0
6      Phoenix, AZ  1996-04   88100.0

(Note that the date is in year-month format) After I covert it into DateTime object so that I could plot it using seaborn, I get the issue of having a date added after the month.

zillowvisualtop5['Year'] = pd.to_datetime(zillowvisualtop5['Year'], format= '%Y-%m')
zillowvisualtop5.head()
Out[165]: 
        City_State       Year     Price
0     New York, NY 1996-04-01  169300.0
1  Los Angeles, CA 1996-04-01  157700.0
2      Houston, TX 1996-04-01   86500.0
3      Chicago, IL 1996-04-01  114000.0
6      Phoenix, AZ 1996-04-01   88100.0

The solution that I've found seems to suggest converting to strftime but I need my time to be in DateTime format so I can plot it using seaborn.

Upvotes: 0

Views: 785

Answers (1)

Roberto Moctezuma
Roberto Moctezuma

Reputation: 54

The problem that you are having is that a DateTime object will always include all the components of a date object and a time object. There is no way to have a DateTime object that only has year and month info (source: here).

However, you can use the matplotlib converters like this:

import pandas as pd
from pandas.plotting import register_matplotlib_converters
import seaborn as sns

cols = ['City', 'Price', 'Month']
data = [['New York', 125, '1996-04'],
        ['New York', 79, '1996-05'],
        ['New York', 85, '1996-06'],
        ['Houston', 90, '1996-04'],
        ['Houston', 95, '1996-05'],
        ['Houston',127, '1996-06']]

df = pd.DataFrame(data, columns = cols)
print (df)

chart = sns.lineplot(x='Month', y='Price', hue='City', data=df)

enter image description here

Does that get the results you were looking for?

Upvotes: 1

Related Questions