py2eviews: EViews & Python
===========================
The purpose of the **py2eviews** package is to make it easier for `EViews <http://www.eviews.com>`_ and Python to talk to each other, so Python programmers can use the econometric engine of EViews directly from Python. This package uses COM to transfer data between Python and EViews. (For more information on COM and EViews, take a look at our `whitepaper on the subject <http://www.eviews.com/download/whitepapers/EViews_COM_Automation.pdf>`_.)
Here's a simple example going from Python to EViews. We're going to use the popular Chow-Lin interpolation routine in EViews using data created in Python. Chow-Lin interpolation is a regression-based technique to transform low-frequency data (in our example, annual) into higher-frequency data (in our example, quarterly). It has the ability to use a higher-frequency series as a pattern for the interpolated series to follow. The quarterly interpolated series is chosen to match the annual benchmark series in one of four ways: first (the first quarter value of the interpolated series matches the annual series), last (same, but for the fourth quarter value), sum (the sum of the first through fourth quarters matches the annual series), and average (the average of the first through fourth quarters matches the annual series).
We're going to create two series in Python using the time series functionality of the **pandas** package, transfer it to EViews, perform Chow-Lin interpolation on our series, and bring it back into Python. The data are taken from [BLO2001]_ in an example originally meant for Denton interpolation.
* Install the **py2eviews** package using your method of choice. For example, head over to the **py2eviews** `package <https://pypi.python.org/pypi/py2eviews>`_ at the `Python Package Index <https://pypi.python.org/pypi>`_ and at a Windows command prompt:
::
$ pip install py2eviews
Or, download the package, navigate to your installation directory, and use:
::
$ python setup.py install
For more details on installation, see our `whitepaper <http://www.eviews.com/download/whitepapers/py2eviews.pdf>`_.
* Start python and create two time series using pandas. We'll call the annual series "benchmark" and the quarterly series "indicator":
.. code-block:: python
>>> import numpy as np
>>> import pandas as pa
>>> dtsa = pa.date_range('1998', periods = 3, freq = 'A')
>>> benchmark = pa.Series([4000.,4161.4,np.nan], index=dtsa, name = 'benchmark')
>>> dtsq = pa.date_range('1998q1', periods = 12, freq = 'Q')
>>> indicator = pa.Series([98.2, 100.8, 102.2, 100.8, 99., 101.6, 102.7, 101.5, 100.5, 103., 103.5, 101.5], index = dtsq, name = 'indicator')
* Load the **py2eviews** package and create a custom COM application object so we can customize our settings. Set `showwindow` (which displays the EViews window) to True. Then call the `PutPythonAsWF` function to create pages for the benchmark and indicator series:
.. code-block:: python
>>> import py2eviews as evp
>>> eviewsapp = evp.GetEViewsApp(instance='new', showwindow=True)
>>> evp.PutPythonAsWF(benchmark, app=eviewsapp)
>>> evp.PutPythonAsWF(indicator, app=eviewsapp, newwf=False)
Behind the scenes, **py2eviews** will detect if the DatetimeIndex of your **pandas** object (if you have one) needs to be adjusted to match EViews' dating customs. Since EViews assigns dates to be the beginning of a given period depending on the frequency, this can lead to misalignment issues and unexpected results when calculations are performed. For example, a DatetimeIndex with an annual 'A' frequency and a date of 2000-12-31 will be assigned an internal EViews date of 2000-12-01. In this case, **py2eviews** will adjust the date to 2000-01-01 before pushing the data to EViews.
* Name the pages of the workfile:
.. code-block:: python
>>> evp.Run('pageselect Untitled', app=eviewsapp)
>>> evp.Run('pagerename Untitled annual', app=eviewsapp)
>>> evp.Run('pageselect Untitled1', app=eviewsapp)
>>> evp.Run('pagerename Untitled1 quarterly', app=eviewsapp)
* Use the EViews ``copy`` command to copy the benchmark series in the annual page to the quarterly page, using the indicator series in the quarterly page as the high-frequency indicator and matching the sum of the benchmarked series for each year (four quarters) with the matching annual value of the benchmark series:
.. code-block:: python
>>> evp.Run('copy(rho=.7, c=chowlins) annual\\benchmark quarterly\\benchmarked @indicator indicator', app=eviewsapp)
* Bring the new series back into Python:
.. code-block:: python
>>> benchmarked = evp.GetWFAsPython(app=eviewsapp, pagename= 'quarterly', namefilter= 'benchmarked')
>>> print(benchmarked)
BENCHMARKED
1998-01-01 867.421429
1998-04-01 1017.292857
1998-07-01 1097.992857
1998-10-01 1017.292857
1999-01-01 913.535714
1999-04-01 1063.407143
1999-07-01 1126.814286
1999-10-01 1057.642857
2000-01-01 1000.000000
2000-04-01 1144.107143
2000-07-01 1172.928571
2000-10-01 1057.642857
* Release the memory allocated to the COM process (this does not happen automatically in interactive mode):
.. code-block:: python
>>> eviewsapp.Hide()
>>> eviewsapp = None
>>> evp.Cleanup()
Note that if you choose not to create a custom COM application object (the `GetEViewsApp` function), you won't need to use the first two lines in the last step. You only need to call `Cleanup()`. If you create a custom object but choose not to show it, you won't need to use the first line (the `Hide()` function).
* If you want, plot everything to see how the interpolated series follows the indicator series:
.. code-block:: python
>>> # load the matplotlib package to plot
import matplotlib.pyplot as plt
>>> # reindex the benchmarked series to the end of the quarter so the dates match those of the indicator series
benchmarked_reindexed = pa.Series(benchmarked.values.flatten(), index = benchmarked.index + pa.DateOffset(months = 3, days = -1))
>>> # plot
fig, ax1 = plt.subplots()
plt.xticks(rotation=70)
ax1.plot(benchmarked_reindexed, 'b-', label='benchmarked')
# multiply the indicator series by 10 to put it on the same axis as the benchmarked series
ax1.plot(indicator*10, 'b--', label='indicator*10')
ax1.set_xlabel('dates')
ax1.set_ylabel('indicator & interpolated values', color='b')
ax1.xaxis.grid(True)
for tl in ax1.get_yticklabels():
tl.set_color('b')
plt.legend(loc='lower right')
ax2 = ax1.twinx()
ax2.set_ylim([3975, 4180])
ax2.plot(benchmark, 'ro', label='benchmark')
ax2.set_ylabel('benchmark', color='r')
for tl in ax2.get_yticklabels():
tl.set_color('r')
plt.legend(loc='upper left')
plt.title("Chow-Lin interpolation: \nannual sum of benchmarked = benchmark", fontsize=14)
plt.show()
.. image:: https://github.com/eviews-support/py2eviews/blob/master/example-python.png?raw=true
:height: 100px
:width: 200px
:scale: 100 %
:align: center
For more information on the **py2eviews** package, including a list of functions, please take a look at our `whitepaper <http://www.eviews.com/download/whitepapers/py2eviews.pdf>`_ on the subject.
References
----------
.. [BLO2001] Bloem, A.M, Dippelsman, R.J. and Maehle, N.O. 2001 Quarterly National Accounts Manual - Concepts, Data Sources, and Compilation. IMF. http://www.imf.org/external/pubs/ft/qna/2000/Textbook/index.htm
Requirements
------------
* **EViews**, of course
* comtypes, numpy, and pandas
Raw data
{
"_id": null,
"home_page": "https://github.com/eviews-support/py2eviews",
"name": "py2eviews",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "eviews econometrics",
"author": "EViews-Support",
"author_email": "support@eviews.com",
"download_url": "https://files.pythonhosted.org/packages/48/bc/54d2edca90a594be41aba8151da0aaed96590b5326ece29ab1d84322ea45/py2eviews-1.0.7.tar.gz",
"platform": null,
"description": "py2eviews: EViews & Python\r\n===========================\r\n\r\nThe purpose of the **py2eviews** package is to make it easier for `EViews <http://www.eviews.com>`_ and Python to talk to each other, so Python programmers can use the econometric engine of EViews directly from Python. This package uses COM to transfer data between Python and EViews. (For more information on COM and EViews, take a look at our `whitepaper on the subject <http://www.eviews.com/download/whitepapers/EViews_COM_Automation.pdf>`_.)\r\n\r\nHere's a simple example going from Python to EViews. We're going to use the popular Chow-Lin interpolation routine in EViews using data created in Python. Chow-Lin interpolation is a regression-based technique to transform low-frequency data (in our example, annual) into higher-frequency data (in our example, quarterly). It has the ability to use a higher-frequency series as a pattern for the interpolated series to follow. The quarterly interpolated series is chosen to match the annual benchmark series in one of four ways: first (the first quarter value of the interpolated series matches the annual series), last (same, but for the fourth quarter value), sum (the sum of the first through fourth quarters matches the annual series), and average (the average of the first through fourth quarters matches the annual series).\r\n\r\nWe're going to create two series in Python using the time series functionality of the **pandas** package, transfer it to EViews, perform Chow-Lin interpolation on our series, and bring it back into Python. The data are taken from [BLO2001]_ in an example originally meant for Denton interpolation.\r\n\r\n* Install the **py2eviews** package using your method of choice. For example, head over to the **py2eviews** `package <https://pypi.python.org/pypi/py2eviews>`_ at the `Python Package Index <https://pypi.python.org/pypi>`_ and at a Windows command prompt:\r\n\r\n:: \r\n\r\n $ pip install py2eviews\r\n\r\nOr, download the package, navigate to your installation directory, and use:\r\n\r\n::\r\n\r\n $ python setup.py install \r\n\r\nFor more details on installation, see our `whitepaper <http://www.eviews.com/download/whitepapers/py2eviews.pdf>`_.\r\n\r\n*\tStart python and create two time series using pandas. We'll call the annual series \"benchmark\" and the quarterly series \"indicator\":\r\n\r\n.. code-block:: python\r\n\r\n >>> import numpy as np \r\n >>> import pandas as pa\r\n >>> dtsa = pa.date_range('1998', periods = 3, freq = 'A')\r\n >>> benchmark = pa.Series([4000.,4161.4,np.nan], index=dtsa, name = 'benchmark')\r\n >>> dtsq = pa.date_range('1998q1', periods = 12, freq = 'Q')\r\n >>> indicator = pa.Series([98.2, 100.8, 102.2, 100.8, 99., 101.6, 102.7, 101.5, 100.5, 103., 103.5, 101.5], index = dtsq, name = 'indicator')\r\n \r\n*\tLoad the **py2eviews** package and create a custom COM application object so we can customize our settings. Set `showwindow` (which displays the EViews window) to True. Then call the `PutPythonAsWF` function to create pages for the benchmark and indicator series:\r\n\r\n.. code-block:: python\r\n\r\n >>> import py2eviews as evp\r\n >>> eviewsapp = evp.GetEViewsApp(instance='new', showwindow=True)\r\n >>> evp.PutPythonAsWF(benchmark, app=eviewsapp)\r\n >>> evp.PutPythonAsWF(indicator, app=eviewsapp, newwf=False)\r\n\r\nBehind the scenes, **py2eviews** will detect if the DatetimeIndex of your **pandas** object (if you have one) needs to be adjusted to match EViews' dating customs. Since EViews assigns dates to be the beginning of a given period depending on the frequency, this can lead to misalignment issues and unexpected results when calculations are performed. For example, a DatetimeIndex with an annual 'A' frequency and a date of 2000-12-31 will be assigned an internal EViews date of 2000-12-01. In this case, **py2eviews** will adjust the date to 2000-01-01 before pushing the data to EViews.\r\n\r\n*\tName the pages of the workfile:\r\n\r\n.. code-block:: python\r\n\r\n >>> evp.Run('pageselect Untitled', app=eviewsapp)\r\n >>> evp.Run('pagerename Untitled annual', app=eviewsapp)\r\n >>> evp.Run('pageselect Untitled1', app=eviewsapp)\r\n >>> evp.Run('pagerename Untitled1 quarterly', app=eviewsapp)\r\n \r\n*\tUse the EViews ``copy`` command to copy the benchmark series in the annual page to the quarterly page, using the indicator series in the quarterly page as the high-frequency indicator and matching the sum of the benchmarked series for each year (four quarters) with the matching annual value of the benchmark series:\r\n\r\n.. code-block:: python\r\n\r\n >>> evp.Run('copy(rho=.7, c=chowlins) annual\\\\benchmark quarterly\\\\benchmarked @indicator indicator', app=eviewsapp)\r\n \r\n*\tBring the new series back into Python:\r\n\r\n.. code-block:: python\r\n\r\n >>> benchmarked = evp.GetWFAsPython(app=eviewsapp, pagename= 'quarterly', namefilter= 'benchmarked')\r\n >>> print(benchmarked)\r\n BENCHMARKED\r\n 1998-01-01 867.421429\r\n 1998-04-01 1017.292857\r\n 1998-07-01 1097.992857\r\n 1998-10-01 1017.292857\r\n 1999-01-01 913.535714\r\n 1999-04-01 1063.407143\r\n 1999-07-01 1126.814286\r\n 1999-10-01 1057.642857\r\n 2000-01-01 1000.000000\r\n 2000-04-01 1144.107143\r\n 2000-07-01 1172.928571\r\n 2000-10-01 1057.642857\r\n\r\n*\tRelease the memory allocated to the COM process (this does not happen automatically in interactive mode):\r\n\r\n.. code-block:: python\r\n\r\n >>> eviewsapp.Hide()\r\n >>> eviewsapp = None\r\n >>> evp.Cleanup()\r\n\r\nNote that if you choose not to create a custom COM application object (the `GetEViewsApp` function), you won't need to use the first two lines in the last step. You only need to call `Cleanup()`. If you create a custom object but choose not to show it, you won't need to use the first line (the `Hide()` function).\r\n\r\n*\tIf you want, plot everything to see how the interpolated series follows the indicator series:\r\n\r\n.. code-block:: python\r\n\r\n >>> # load the matplotlib package to plot\r\n import matplotlib.pyplot as plt\r\n >>> # reindex the benchmarked series to the end of the quarter so the dates match those of the indicator series\r\n benchmarked_reindexed = pa.Series(benchmarked.values.flatten(), index = benchmarked.index + pa.DateOffset(months = 3, days = -1))\r\n >>> # plot\r\n fig, ax1 = plt.subplots()\r\n plt.xticks(rotation=70)\r\n ax1.plot(benchmarked_reindexed, 'b-', label='benchmarked')\r\n # multiply the indicator series by 10 to put it on the same axis as the benchmarked series\r\n ax1.plot(indicator*10, 'b--', label='indicator*10') \r\n ax1.set_xlabel('dates')\r\n ax1.set_ylabel('indicator & interpolated values', color='b')\r\n ax1.xaxis.grid(True)\r\n for tl in ax1.get_yticklabels():\r\n tl.set_color('b')\r\n plt.legend(loc='lower right')\r\n ax2 = ax1.twinx()\r\n ax2.set_ylim([3975, 4180])\r\n ax2.plot(benchmark, 'ro', label='benchmark')\r\n ax2.set_ylabel('benchmark', color='r')\r\n for tl in ax2.get_yticklabels():\r\n tl.set_color('r')\r\n plt.legend(loc='upper left')\r\n plt.title(\"Chow-Lin interpolation: \\nannual sum of benchmarked = benchmark\", fontsize=14)\r\n plt.show()\r\n\r\n.. image:: https://github.com/eviews-support/py2eviews/blob/master/example-python.png?raw=true\r\n :height: 100px\r\n :width: 200px\r\n :scale: 100 %\r\n :align: center\r\n\r\nFor more information on the **py2eviews** package, including a list of functions, please take a look at our `whitepaper <http://www.eviews.com/download/whitepapers/py2eviews.pdf>`_ on the subject.\r\n\r\nReferences\r\n----------\r\n.. [BLO2001] Bloem, A.M, Dippelsman, R.J. and Maehle, N.O. 2001 Quarterly National Accounts Manual - Concepts, Data Sources, and Compilation. IMF. http://www.imf.org/external/pubs/ft/qna/2000/Textbook/index.htm\r\n\r\nRequirements\r\n------------\r\n* **EViews**, of course\r\n* comtypes, numpy, and pandas\r\n",
"bugtrack_url": null,
"license": "GPLv3",
"summary": "Data import/export and EViews function calls from Python",
"version": "1.0.7",
"project_urls": {
"Homepage": "https://github.com/eviews-support/py2eviews"
},
"split_keywords": [
"eviews",
"econometrics"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "b318212fe1926d7d95b7f7a6f87833f7392aedcc093ffb75ec0c79a972f26e02",
"md5": "3aa9f78c1b668b95101fff0b805196a8",
"sha256": "384578389a7f86b672985e2be40730b47e7349a35113aa3f421c3c1a2ece5d05"
},
"downloads": -1,
"filename": "py2eviews-1.0.7-py3-none-any.whl",
"has_sig": false,
"md5_digest": "3aa9f78c1b668b95101fff0b805196a8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 11284,
"upload_time": "2024-08-22T21:17:00",
"upload_time_iso_8601": "2024-08-22T21:17:00.118809Z",
"url": "https://files.pythonhosted.org/packages/b3/18/212fe1926d7d95b7f7a6f87833f7392aedcc093ffb75ec0c79a972f26e02/py2eviews-1.0.7-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "48bc54d2edca90a594be41aba8151da0aaed96590b5326ece29ab1d84322ea45",
"md5": "737426057d293443e17bfc8ef5cdeba0",
"sha256": "55a6d4a3e4e949073b792c20075cdcf2c63049c6c5b8a1c39f30194b36882621"
},
"downloads": -1,
"filename": "py2eviews-1.0.7.tar.gz",
"has_sig": false,
"md5_digest": "737426057d293443e17bfc8ef5cdeba0",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 54162,
"upload_time": "2024-08-22T21:17:01",
"upload_time_iso_8601": "2024-08-22T21:17:01.929855Z",
"url": "https://files.pythonhosted.org/packages/48/bc/54d2edca90a594be41aba8151da0aaed96590b5326ece29ab1d84322ea45/py2eviews-1.0.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-22 21:17:01",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "eviews-support",
"github_project": "py2eviews",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "py2eviews"
}