0

I have data in this format:

2017-08-01,5.6
2017-09-01,5.6
2017-10-01,5.6
2017-11-01,5.5
2017-12-01,5.5
2018-01-01,5.4
2018-02-01,5.4

This data variable was created in this way:

data= fred.get_series('LMUNRRTTDEM156S').to_csv()

I have successfully inserted the data into separate columns and rows in a csv file but how could i filter the data variable to say: "only write the rows with a date later than 2010-01-01"? Also how can i define column names, i tried a simple write f.write('col1', 'col2') but this failed?

with open("fileName.csv", "w") as f:
    f.write(data)
    f.close()

Full code:

from fredapi import Fred
import csv
import pandas as pd

fred= Fred(api_key= 'XYZ')
data= fred.get_series('LMUNRRTTDEM156S').to_csv()

with open("fileName.csv", "w") as f:
    #f.write('hello', 'world')
    f.write(data)
    f.close()

Any help would be greatly appreciated

3
  • data is a string so, can you share how you built it ? edit your post and add more code Commented May 18, 2021 at 10:50
  • Can you use libraries such as pandas? Commented May 18, 2021 at 10:50
  • Are we to assume that you are using this API? github.com/mortada/fredapi/tree/master/fredapi In which case the get_series data returns a Pandas Series object Commented May 18, 2021 at 10:55

2 Answers 2

0

According to the fredapi source code, you can specify an observation_start parameter to the get_series method.

I'd try the following

fred.get_series('LMUNRRTTDEM156S', observation_start="2010-01-01").to_csv('filename.csv')

This should filter the data to only show observations after the specified date.

To add a header, specify this when you output the csv

fred.get_series('LMUNRRTTDEM156S', observation_start="2010-01-01").to_csv('filename.csv', header=['Date','Values'])
Sign up to request clarification or add additional context in comments.

2 Comments

i get a ValueError: Writing 1 cols but got 2 aliases when i use your second code insert ie the one with the header=['Date','Values']. Thanks again
@danielstafford Oops! try header=['Value'], index_label='Date' as the arguments instead
0

Use

  • observation_start param to specify the date from which retrieve the data

  • use header and index_label to name the columns for the csv dumping

from fredapi import Fred

fred = Fred(api_key=API_KEY)
data = fred.get_series('LMUNRRTTDEM156S', observation_start="2010-01-01")

data.to_csv('filename2.csv', header=['value'], index_label='date')

File will be like

date,value
2010-01-01,8.0
2010-02-01,8.0
2010-03-01,8.0

To join multiple Series use pandas.concat

data = fred.get_series('LMUNRRTTDEM156S', frequency='m', observation_start="2010-01-01")
data1 = fred.get_series('DEUCPIALLMINMEI', frequency='m', observation_start="2010-01-01")

df = pd.concat([data, data1], axis=1).rename(columns={0: 'Germany Unemployment', 1: 'Germany Inflation'})
df.to_csv('german.csv', index_label="date")

DataFrame.fillna can be useful in case one Series have some missing values

df.fillna(0).to_csv('german.csv', index_label="date")

Giving

date,Germany Unemployment,Germany Inflation
2010-01-01,8.0,92.3591431905318
2010-02-01,8.0,92.732311445847
2010-03-01,8.0,93.1987717649912

5 Comments

Since the OP seems to be using the "fredapi" the data that they have is already a pandas Series object so converting the raw text back to a Dataframe isn't necessary.
@scotty3785 yep just saw that, I'm editing
@scotty3785 thanks for all the help. I was wondering, with 2 series from the FRED API how could I put them both into the same csv file and maintain the ordering based on the associated date, ie the new csv file would have 3 columns, 1 for date(the PK), second the attribute from series1 and third for the attribute in series3. To be clear: series1 format: date, attribute. series2 format: date, attribute.
i currently have the code to retrieve the 2 series but unsure how to merge them on their date: from fredapi import Fred import pandas as pd fred= Fred(api_key= 'API_KEY') data = fred.get_series('LMUNRRTTDEM156S', frequency = 'm', observation_start="2010-01-01") data.to_csv('germanU.csv', header=['Germany Unemployment'], index_label='date') data1 = fred.get_series('DEUCPIALLMINMEI', frequency = 'm', observation_start="2010-01-01") data1.to_csv('germanInflation.csv', header=['Germany Inflation'], index_label='date')
@danielstafford Sound like a new question to me. And this isn't my answer, it is from azro.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.