{"id":142,"date":"2016-12-08T19:46:40","date_gmt":"2016-12-08T19:46:40","guid":{"rendered":"http:\/\/debian-economist.eu\/wp\/?p=142"},"modified":"2016-12-08T19:46:40","modified_gmt":"2016-12-08T19:46:40","slug":"plotting-time-series-in-python-is-so-simple","status":"publish","type":"post","link":"https:\/\/debian-economist.eu\/wp\/?p=142","title":{"rendered":"Plotting time series in python is so simple"},"content":{"rendered":"<p>As an econ student, I was more inclined to code in VBA or in R. However, I had my first indirect contact with python through sage math. It is a powerful solver written in python. The sage language share some similarities with python. The software is very attractive because you can call functions from different software packages (simpy, R, maxima, etc&#8230;) within a unique platform.<\/p>\n<p>Alas, sagemath is not ready on Debian which is the OS of my workstation. I needed to plot time series. Therefore, why would I not try python?<\/p>\n<p>I found this <a title=\"Import CSV into Python using Pandas\" href=\"https:\/\/spectraldifferences.wordpress.com\/2014\/04\/08\/import-csv-into-python-using-pandas\/\" target=\"_blank\">very good post<\/a> of a blog titled \u00ab\u00a0Spectral differences\u00a0\u00bb. I would like to import a csv files with futures price inside. You might be european as me. Therefore, your csv file uses semi-colon and not the comma as a separator. Just look at the <a title=\"pandas.read_csv\" href=\"http:\/\/pandas.pydata.org\/pandas-docs\/stable\/generated\/pandas.read_csv.html\" target=\"_blank\">pandas documentation about the function \u00ab\u00a0read_csv\u00a0\u00bb<\/a>. It is very simple. To import a CSV with a semi-colon as a separator, we get:<br \/>\n<code><br \/>\nimport pandas as pd<br \/>\ndata_df = pd.read_csv('us_ng_1998_2014.csv', sep=';')<br \/>\n<\/code><br \/>\nAs you can see,\u00a0 you do not have to declare object in python. You can directly defined the content of an object with the equal sign (\u00ab\u00a0=\u00a0\u00bb) and python will determine what is. Here, it sees that \u00ab\u00a0data_df\u00a0\u00bb is a a dataframe. Pandas that we rename as \u00ab\u00a0pd\u00a0\u00bb to be more comfortable is a library. All the functions are attributes of this library such as \u00ab\u00a0library.function(inputs)\u00a0\u00bb.<\/p>\n<p>Now, we can use the library matplotlib to plot the dataframe. It is important to notice that we plot time series. First, matplotlib have to recognize the date format. It is not automatic if the dates are in the european format. I adapted this <a title=\"Plot pandas dates in matplotlib\" href=\"https:\/\/stackoverflow.com\/questions\/25416955\/plot-pandas-dates-in-matplotlib\" target=\"_blank\">stackoverflow issue<\/a>:<br \/>\n<code>data_df['Unnamed: 0'] = pd.to_datetime(data_df['Unnamed: 0'], format='%d\/%m\/%Y')<br \/>\ndata_df.set_index(['Unnamed: 0'],inplace=True)<\/code><br \/>\nUnnamed:0 is the name given to my first column which is without name. The first line directly transforms the column. So inside the string of the format, you have to indicate the order. %d is for days, %m for month and %Y for year. If you have a slash in your csv file, writes slash. If it is dash, write dash. The second line sets the index. So if you print a column, the index will appear next to the values. I recommand you to replace commas by points for the decimal values. <a title=\"IPython + Pandas Can't plot data from .csv\" href=\"https:\/\/stackoverflow.com\/questions\/18697644\/ipython-pandas-cant-plot-data-from-csv\" target=\"_blank\">This stackoverflow issue was very useful<\/a>. The conversion is pretty simple in our case:<\/p>\n<p><code>data_df = data_df.replace(',', '.', regex=True)<\/code><\/p>\n<p>If the matlib plots does not recognize the values as numeric, you can convert them with <a title=\"pandas.DataFrame.convert_objects\" href=\"pandas.pydata.org\/pandas-docs\/version\/0.17.1\/generated\/pandas.DataFrame.convert_objects.html\" target=\"_blank\">convert_objects<\/a>.<\/p>\n<p>At least, I managed to write pretty easily my first python script:<br \/>\n<code><br \/>\nimport pandas as pd<br \/>\ndata_df = pd.read_csv('us_ng_1998_2014.csv', sep=';')<br \/>\n#print(data_df.columns)<br \/>\nimport matplotlib.pyplot as plt<br \/>\ndata_df['Unnamed: 0'] = pd.to_datetime(data_df['Unnamed: 0'], format='%d\/%m\/%Y')<br \/>\ndata_df = data_df.replace(',', '.', regex=True)<br \/>\ndata_df.set_index(['Unnamed: 0'],inplace=True)<br \/>\n#print data_df['1M'] #check the index<br \/>\ndata_df = data_df.convert_objects(convert_numeric=True)<br \/>\n#print data_df.dtypes #check the types<br \/>\ndata_df['1M'].plot() #plot the column for my first column<br \/>\n#data_df.plot() #plot everything<br \/>\nplt.show()<br \/>\n<\/code><\/p>\n<p>Views: 938<\/p>","protected":false},"excerpt":{"rendered":"<p>As an econ student, I was more inclined to code in VBA or in R. However, I had my first indirect contact with python through sage math. It is a powerful solver written in python. The sage language share some similarities with python. The software is very attractive because you can call functions from different [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[103,102,104,100,101],"class_list":["post-142","post","type-post","status-publish","format-standard","hentry","category-hints","tag-matplotlib","tag-pandas","tag-plot","tag-python","tag-time-series"],"_links":{"self":[{"href":"https:\/\/debian-economist.eu\/wp\/index.php?rest_route=\/wp\/v2\/posts\/142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/debian-economist.eu\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/debian-economist.eu\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/debian-economist.eu\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/debian-economist.eu\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=142"}],"version-history":[{"count":9,"href":"https:\/\/debian-economist.eu\/wp\/index.php?rest_route=\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":151,"href":"https:\/\/debian-economist.eu\/wp\/index.php?rest_route=\/wp\/v2\/posts\/142\/revisions\/151"}],"wp:attachment":[{"href":"https:\/\/debian-economist.eu\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/debian-economist.eu\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/debian-economist.eu\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}