Pandas23 change the way it treats multindex labels for xticklabels? - python

This fragment of code demonstrates a problem I've encountered having switched from pandas 0.19.2 to 0.23.0: df.plot() is not correctly converting the multindex to xticklabels.
import pandas as pd
import datetime
import numpy as np
import dateutil.parser
import dateutil.parser
import matplotlib as plt
%matplotlib inline
import platform
print(f"pandas version: {pd.__version__}")
print(f"python version: {platform.python_version()}")
#create the main dataframe
dt = pd.DatetimeIndex(start='2010-1-1', end = '2010-12-31', freq='m')
dt2 = pd.DatetimeIndex(start='2011-1-1', end = '2011-1-10', freq='d')
mi = pd.MultiIndex.from_product([dt,dt2], names=['assessment_date', 'contract_date'])
df = pd.DataFrame(index=mi)
df['foo']=7
df.plot(rot=50)
When I originally used this code, the result looked like:
But now I use pandas 0.23 and the ticklabels are incorrect
Not sure why this is occurring, or where to look for the issue.

Related

Pandas converting sub-columns into rows

I have added a picture of my dataframe. There are sub-columns names which are 'GBPEUR=X' 'GBPJPY=X' and 'USDMXN=X'. I would like to take these sub-headings and turn them into different rows. Any idea of how to do this as cant find anything else on the internet? Code below:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import yfinance as yf
%matplotlib inline
df = yf.Tickers('GBPJPY=X GBPEUR=X USDMXN=X')
currencies = df.history(period='max')

How to Convert/read the column which is in integer into string value in python pandas dataframe for plotting in plotly?

I have a csv file in which a column is having integer value, i want to convert/read it as string so that i can plot that to graph using python plotly library. I will update the code below here.
please someone help me to convert the column values into string.
CSV File:
build ID,Pass Percentage,original_pass_percent
420693,97,97
420543,97,97
421009,98,98
420638,100,100
420462,92,92
421382,100,100
Here i want to convert the build ID column into a string so that i can get that on the graph using python plotly
code:
import pandas as pd
import plotly
import plotly.express as px
import numpy as np
import matplotlib.pyplot as plt
from pandas import Series, DataFrame
from numpy import nan as NA
import glob
from pathlib import Path
import plotly.graph_objects as go
from flask import Flask, render_template
import json
import plotly
import chart_studio.plotly as py
import plotly.graph_objs as go
from flask import Flask, make_response
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
p = Path(r'c:\Users\shivarad\Documents') # path to files
files = list(p.rglob('*graph22.csv')) # get files
fig = go.Figure()
for f in files:
file_name = f.stem
df = pd.read_csv(f, dtype={'Pass Percentage': str, 'original_pass_percent':int})
df = pd.read_csv(f, dtype={'Pass Percentage': int, 'build ID': str})
df['build ID']=df['build ID'].astype(str)
df = df.sort_values(by=['build ID'], axis = 0)
print(df.head())
fig.add_trace(go.Scatter(x=df['build ID'], y=df['Pass
Percentage'],mode='lines+markers',name=file_name))
fig.update_layout(title='Pass Percent VS Build ID of all the Test Suites',plot_bgcolor='rgb(230,
230,230)',showlegend=True)
fig.show()
Thank you

Convert and plot date and time with pandas or numpy

Trying to plot date and time using pandas. 'dt' and 'quality'
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns
%matplotlib inline
data = pd.read_csv('pontina_FCD.csv')
I've tried lots of options about transfer date, but sill facing the error.
Pandas has methods that support plotting.
Can you try the following:
data.index = pd.to_datetime(data['dt'], format='%d/%m/%Y %H:%M')
data['quality'].plot()
plot.show()

How to plot dates from a csv file?

I am new to python and I am having some issues to plot my dates from a csv file.
The code is the following:
import pandas as pd
import numpy as np
import statsmodels.api as sm
from pandas import DataFrame
import matplotlib.pyplot as plt
df = pd.read_csv(r"file.csv",index_col=0)
print(df.describe())
BHSI_cycle, BHSI_trend = sm.tsa.filters.hpfilter(df['BHSI-TCA'])
df['BHSI_trend'] = BHSI_trend
df['BHSI_cycle'] = BHSI_cycle
BHSI_plot = df[['BHSI-TCA','BHSI_trend']].plot(figsize=(12,10))
plt.show(BHSI_plot)
BHSI_plot2 = df[['BHSI_cycle']].plot(figsize=(12,10))
plt.show(BHSI_plot2)
And the CSV file is:
Date BHSI-TCA
23/05/2006 14821
25/05/2006 14878
30/05/2006 14837
How can I plot the dates?
Try parsing dates properly when you import from csv.
df = pd.read_csv(r"file.csv", index_col=0, parse_dates=<your_date_column>)

How can I show True/False graph on Python

I have a csv file and I want to show this data on grap. I have date,place and status data but I don't need place so I fetch data like this.
And going like this
Here is my code. How can I get a graph with 1-0 values according to date value. Which method should I use ? Thanks
import pandas as pd from pandas
import DataFrame
import datetime
import pandas.io.data
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d
import Axes3D import pylab rows_list=[] df=pd.read_csv('filepath',header=None,parse_dates=True,pr‌​efix='column')
for row in df.iterrows():
if row[1][1]=='Beweging in de living':
if row[1][2]=='OPEN': rows_list.append([row[1][0],'1'])
else: rows_list.append([row[1][0],'0'])
df2 = pd.DataFrame(rows_list)
df3=df2.set_index(0)
print df3 plt.plot(df3)
plt.show()

Categories

Resources