{"id":78,"date":"2023-07-04T15:45:00","date_gmt":"2023-07-04T13:45:00","guid":{"rendered":"https:\/\/beebole.com\/blog\/?p=43"},"modified":"2025-09-18T10:57:52","modified_gmt":"2025-09-18T08:57:52","slug":"python-for-finance-examples","status":"publish","type":"post","link":"https:\/\/beebole.com\/blog\/python-for-finance-examples","title":{"rendered":"Python for finance: Unlocking the power of data analysis"},"content":{"rendered":"\n<p>Are you exploring the intriguing world of <strong>python for finance<\/strong>? Whether you aspire to work in the financial industry, or wish to advance your existing career, mastering Python is a key step forward. In today&#8217;s fast-paced financial arena, Python programming is a crucial skill that top institutions seek in their professionals. Dive into this article to discover the four major reasons behind Python&#8217;s rising popularity in the financial industry, and how it can propel your career to new heights.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-programming-for-finance-explained\">Universal languages &amp; how Python programming for finance fits in<\/h2>\n\n\n\n<p>There are not too many universal languages. When it comes to spoken and written languages, English can be considered universal. If you are not a native speaker, English often is the first foreign language that you learn. And there are good reasons to do so. First, most of the published texts, such as news articles or books, are written in English. Secondly, you can get by with English basically around the world. Mathematics is a universal symbolic language that is also understood and used around the world and across many domains.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-a-deeper-look-at-python\">A deeper look at Python<\/h3>\n\n\n\n<p>When it comes to programming languages, <a href=\"https:\/\/python.org\" target=\"_blank\" rel=\"noopener\">Python<\/a> has reached a similar status in finance and other domains. Python is used in basically every area of the financial industry, be it for financial data science, machine learning, credit ratings, trading, asset management, pricing, risk, or more administrative tasks. In general, the reach of Python is indeed universal in that it is used around the world, across basically all domains, and for basically any task that requires some form of programming and data processing.<\/p>\n\n\n\n<div\n    class=\"montserrat-font my-5 mx-auto p-4 p-lg-5 position-relative bbl_customer_story_blurb\"\n>\n  <svg\n    class=\"bk-svg-top position-absolute w-100\"\n    fill=\"none\"\n    height=\"114\"\n    viewBox=\"0 0 724 114\"\n    width=\"724\"\n    xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n    <path d=\"M-18.2595 85.4232C15.6765 58.6399 103.632 22.1092 183.964 90.253C284.38 175.433 358.663 -39.1831 421.703 17.5374C484.743 74.2578 573.058 -90.3762 669.372 0.952557C746.423 74.0156 852.42 -37.4955 895.787 -102.384\" stroke=\"#F9F8FD\" stroke-width=\"7\"\/>\n  <\/svg>\n\n  <svg\n    class=\"bk-svg-bottom position-absolute w-100\"\n    fill=\"none\"\n    height=\"113\"\n    viewBox=\"0 0 724 113\"\n    width=\"724\"\n    xmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n  >\n    <path d=\"M899.19 176.521C864.862 150.242 776.377 115.015 697.059 184.338C597.913 270.991 520.468 57.496 458.273 115.141C396.077 172.787 305.34 9.47523 210.386 102.217C134.422 176.41 26.7898 66.4765 -17.5308 2.23568\" stroke=\"#F9F8FD\" stroke-width=\"7\"\/>\n  <\/svg>\n\n  <div class=\"position-relative\">\n    \n    <div class=\"bbl-csb-text\">\n      <p data-start=\"215\" data-end=\"478\">Rancho BioSciences isn\u2019t just tracking hours\u2014they\u2019re engineering data-driven foresight. By combining Beebole\u2019s real-time time tracking with Python, their team builds custom dashboards and burndown charts that make forecasting sharper and decision-making faster.<\/p>\n<p data-start=\"480\" data-end=\"777\">With Beebole, they\u2019re able to:<br data-start=\"510\" data-end=\"513\" \/>\ud83d\ude80 Transform time and budget data into actionable Python models<br data-start=\"576\" data-end=\"579\" \/>\ud83d\ude80 Align 130+ simultaneous projects under one reliable source of truth<br data-start=\"649\" data-end=\"652\" \/>\ud83d\ude80 Forecast with confidence instead of chasing outdated reports<br data-start=\"715\" data-end=\"718\" \/>\ud83d\ude80 Empower leadership with clarity backed by real numbers<\/p>\n<p data-start=\"480\" data-end=\"777\">This is what forecasting looks like when data and finance finally click.<\/p>\n    <\/div>\n\n          <a class=\"bbl-csb-link\" href=\"https:\/\/beebole.com\/blog\/how-to-avoid-project-cost-overruns\/\">\n        Read the case study\n        <svg width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n          <path d=\"M2 8H14\" stroke=\"#464646\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"\/>\n          <path d=\"M8 2L14 8L8 14\" stroke=\"#464646\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"\/>\n        <\/svg>\n      <\/a>\n      <\/div>\n<\/div>\n\n\n<p>Against this background, basically all financial institutions expect from their new hires and existing staff at least a certain level of Python programming. For specific roles, such as quantitative developers or researchers, Python skills are usually required these days. For other roles, such as in trading or risk management financial institutions offer Python training programs to reflect its increasing importance. Therefore, it for sure pays off to learn Python early on and gain relevant experience in using it for financial use cases.<\/p>\n\n\n\n<p>Without a doubt, there are still several other programming languages in use in finance. For example, C++, as a compiled programming language, is still very popular when code execution speed and performance are of the essence. On the other hand, domain-specific languages, such as R for statistical applications, are also widely used.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"why-python-for-finance-important\">Why is Python so huge in finance?<\/h2>\n\n\n\n<p>But what are the major reasons why Python has become basically omnipresent in finance and has in many instances replaced other, more focused, programming languages? Some of the basic characteristics of Python are shared by several other languages as well, such as being open-source, interpreted, high-level, and dynamically typed. This alone cannot explain its rise in finance.<\/p>\n\n\n\n<p>In what follows, four arguments are discussed for why Python has become so dominant in finance. The first two are presented in the form of what you could call the power packages in Python: NumPy and pandas. The second two are related to important aspects in finance: code performance and efficient input-output (IO) operations. But first a simple use case illustrating the power of Python for financial data analysis.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-for-finance-controllers-analysts-better-than-excel\">Is Python for finance controllers and analysts better than Excel?<\/h2>\n\n\n\n<p>A financial controller within an organization can absolutely work with both Microsoft Excel and Python at the same time. Both tools have their unique strengths and can be used complementarily.<\/p>\n\n\n\n<p>Excel is widely used in the business world for <a href=\"https:\/\/beebole.com\/blog\/how-to-calculate-project-profitability\/\" data-type=\"post\" data-id=\"8571\">simpler data analysis<\/a>, financial modeling, <a href=\"https:\/\/beebole.com\/blog\/budget-vs-actuals-template-microsoft-excel\/\" data-type=\"post\" data-id=\"7687\">budget vs actuals<\/a>, and quick calculations. It offers an intuitive interface and allows for the easy manipulation of data, making it accessible for those without a coding background. Finance professionals who use Excel might also be interested in learning about using <a href=\"https:\/\/beebole.com\/blog\/microsoft-fabric-for-finance-professionals\/\">Microsoft Fabric<\/a>.<\/p>\n\n\n\n<p>On the other hand, Python is a powerful programming language favored by analysts and data scientists for <strong>more complex tasks<\/strong>. It&#8217;s excellent for large-scale data analysis, automating repetitive tasks, building sophisticated models, and conducting advanced statistical analysis. <strong>Python also has the ability to read and write Excel files, which can be useful for integrating the two tools<\/strong>.<\/p>\n\n\n\n<p>A financial analyst might use Excel for quick and simple analyses, data visualization, or when sharing data with others in a user-friendly format. Meanwhile, Python would come into play for more advanced analysis or when dealing with large datasets that Excel cannot efficiently handle. In other words, Python and Excel can be used in tandem, leveraging the strengths of each depending on the task at hand.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-finance-professionals-use-python-use-case\">How do finance professionals use Python? A simple use case<\/h2>\n\n\n\n<p>The returns of the share prices of stocks in financial markets are important measures for many different financial applications. In this context, consider a relatively simple question: \u201cHow did the Apple Stock perform in 2021 compared to the S&amp;P 500 stock index?\u201d.<\/p>\n\n\n\n<p>Working with traditional tools used in finance, such as Microsoft Excel, is often a tedious experience with regard to answering such questions. An analyst would need to download the data, for example, in the form of a CSV (comma-separated value) file containing the time series of prices for the financial instruments of interest. The analyst then needs to import the data into an Excel spreadsheet, which usually involves some configuration and several formatting and layout-related steps. <\/p>\n\n\n\n<p>In addition, such data sets often comprise thousands of rows\u2009\u2014\u2009something not efficient to process within a spreadsheet since it involves constant up- and down-scrolling. Once the data is ready, the analyst needs to find the relevant dates in the spreadsheet, that is the 31. December of 2020 and 2021, and pick the prices at these dates for Apple and the S&amp;P 500. Based on these prices, the returns for 2021 can then be calculated and compared.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"comes-in\">Where Python packages come in<\/h3>\n\n\n\n<p>On the other hand, an analyst who is proficient in using Python can answer such questions\u2009\u2014\u2009and even more complex ones\u2009\u2014\u2009in a much more efficient manner. Leveraging powerful Python packages such as pandas (see section <a href=\"https:\/\/certificate.tpq.io\/why.html#pandas_pack\" target=\"_blank\" rel=\"noopener\">Pandas Package<\/a>) boils down the whole analysis to three relatively simple lines of code:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Importing the pandas package <\/li>\n\n\n\n<li>Retrieving the CSV data file <\/li>\n\n\n\n<li>Calculating the net returns<\/li>\n<\/ol>\n\n\n\n<p>Executing the three lines of the following Python code shows that Apple has outperformed the S&amp;P 500 stock index in 2021 by about 7 percentage points.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>In &#91;1&#93;: &lt;strong>import&lt;\/strong> pandas &lt;strong>as &lt;\/strong>pd\n\nIn &#91;2&#93;: data = pd.read_csv('https:\/\/certificate.tpq.io\/mlfin.csv',\n                    index_col=0, parse_dates=True)\n\nIn &#91;3&#93;: (data[&#91;'AAPL.O', '.SPX'&#93;].loc&#91;'2021-12-31'&#93; \/\n         data[&#91;'AAPL.O', '.SPX'&#93;].loc&#91;'2020-12-31'&#93;) - 1\nOut&#91;3&#93;: AAPL.O    0.338232\n        .SPX      0.268927\n        dtype: float64\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;import&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\"> pandas <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;as<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\">pd<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\">&#93;: data <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> pd.read_csv(<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">https:\/\/certificate.tpq.io\/mlfin.csv<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #F6F6F4\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">                    <\/span><span style=\"color: #FFB86C; font-style: italic\">index_col<\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #FFB86C; font-style: italic\">parse_dates<\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #BF9EEE\">True<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">3<\/span><span style=\"color: #F6F6F4\">&#93;: (data[&#91;<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">AAPL.O<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">.SPX<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #F6F6F4\">&#93;].loc&#91;<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">2021-12-31<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #F6F6F4\">&#93; <\/span><span style=\"color: #F286C4\">\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         data[&#91;<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">AAPL.O<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">.SPX<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #F6F6F4\">&#93;].loc&#91;<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">2020-12-31<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #F6F6F4\">&#93;) <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">Out&#91;<\/span><span style=\"color: #BF9EEE\">3<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #BF9EEE\">AAPL<\/span><span style=\"color: #F6F6F4\">.O    <\/span><span style=\"color: #BF9EEE\">0.338232<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        .<\/span><span style=\"color: #BF9EEE\">SPX<\/span><span style=\"color: #F6F6F4\">      <\/span><span style=\"color: #BF9EEE\">0.268927<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        dtype: float64<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-packages-for-finance-explained\">Python packages for finance: Which Python is best for finance controllers? <\/h2>\n\n\n\n<p>A Python package is essentially a directory of Python scripts, each of which defines functions, methods, and types. These <strong>scripts<\/strong>, known as <em>modules<\/em>, <strong>together form a package<\/strong>. The advantage is that these packages can be &#8220;imported&#8221; into another Python script, providing <strong>reusability of code and efficiency in managing large codebases<\/strong>. This aids in maintaining an organized, modular, and more manageable code structure.<\/p>\n\n\n\n<p>In the context of financial analysis, Python packages like pandas, NumPy, and SciPy offer powerful data manipulation and analysis capabilities. As explained below, pandas will enable you efficient handling and manipulation of financial data sets, NumPy will provide you with high-performance mathematical computation abilities. <\/p>\n\n\n\n<p>Moreover, packages like matplotlib and seaborn facilitate data visualization, essential for interpreting financial trends and patterns. Also, SciPy allows for advanced statistical analysis. Hence, for finance controllers, these Python packages can significantly streamline financial data analysis and decision-making processes for organizations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-implement-numpy-package\">How to implement the NumPy package<\/h2>\n\n\n\n<p><strong>Finance to a large extent is applied mathematics<\/strong>. Therefore, a programming language that supports mathematical and numerical operations well has an advantage in finance. The NumPy package (see <a href=\"https:\/\/numpy.org\/\" target=\"_blank\" rel=\"noopener\">https:\/\/numpy.org<\/a>) is central to the implementation of financial algorithms in Python. On its webpage, you find the following text:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more.<\/em><\/p>\n<\/blockquote>\n\n\n\n<p>In particular, NumPy allows you to implement financial algorithms in vectorized fashion. This means that you can avoid, to a large extent, loops on the Python level. Consider the following simple example which implements, in two different ways, the scalar multiplication of a vector in pure Python. Both ways include the looping over the single elements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>In &#91;4&#93;: v = list(range(10))\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0v\nOut&#91;4&#93;: &#91;0, 1, 2, 3, 4, 5, 6, 7, 8, 9&#93;\n\nIn &#91;5&#93;: w = list()\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;strong>for &lt;\/strong>i &lt;strong>in &lt;\/strong>v:\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0w.append(2 * i)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0w\nOut&#91;5&#93;: &#91;0, 2, 4, 6, 8, 10, 12, 14, 16, 18&#93;\n\nIn &#91;6&#93;: &#91;2 * i for i in v&#93;\nOut&#91;6&#93;: &#91;0, 2, 4, 6, 8, 10, 12, 14, 16, 18&#93;<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">4<\/span><span style=\"color: #F6F6F4\">&#93;: v <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1; font-style: italic\">list<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #BF9EEE\">10<\/span><span style=\"color: #F6F6F4\">))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0v<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">Out&#91;<\/span><span style=\"color: #BF9EEE\">4<\/span><span style=\"color: #F6F6F4\">&#93;: &#91;<\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">3<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">4<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">5<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">6<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">7<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">8<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">9<\/span><span style=\"color: #F6F6F4\">&#93;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">5<\/span><span style=\"color: #F6F6F4\">&#93;: w <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1; font-style: italic\">list<\/span><span style=\"color: #F6F6F4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;for<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\">i <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\">v:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0w.append(<\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #F6F6F4\"> i)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0w<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">Out&#91;<\/span><span style=\"color: #BF9EEE\">5<\/span><span style=\"color: #F6F6F4\">&#93;: &#91;<\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">4<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">6<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">8<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">10<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">12<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">14<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">16<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">18<\/span><span style=\"color: #F6F6F4\">&#93;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">6<\/span><span style=\"color: #F6F6F4\">&#93;: &#91;<\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">for<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">in<\/span><span style=\"color: #F6F6F4\"> v&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">Out&#91;<\/span><span style=\"color: #BF9EEE\">6<\/span><span style=\"color: #F6F6F4\">&#93;: &#91;<\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">4<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">6<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">8<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">10<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">12<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">14<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">16<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">18<\/span><span style=\"color: #F6F6F4\">&#93;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Now compare the implementation with NumPy in the following code. It boils down to a single vectorized operation that resembles the symbolics of scalar multiplication in mathematics. There is no loop on the Python level anymore.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>In &#91;7&#93;: &lt;strong>import &lt;\/strong>numpy &lt;strong>as &lt;\/strong>np\n\nIn &#91;8&#93;: v = np.arange(10)\n        v\nOut&#91;8&#93;: array(&#91;0, 1, 2, 3, 4, 5, 6, 7, 8, 9&#93;)\n\nIn &#91;9&#93;: 2 * v\nOut&#91;9&#93;: array(&#91; 0,  2,  4,  6,  8, 10, 12, 14, 16, 18&#93;)\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">7<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;import<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\">numpy <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;as<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\">np<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">8<\/span><span style=\"color: #F6F6F4\">&#93;: v <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> np.arange(<\/span><span style=\"color: #BF9EEE\">10<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        v<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">Out&#91;<\/span><span style=\"color: #BF9EEE\">8<\/span><span style=\"color: #F6F6F4\">&#93;: array(&#91;<\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">3<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">4<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">5<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">6<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">7<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">8<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">9<\/span><span style=\"color: #F6F6F4\">&#93;)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">9<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #F6F6F4\"> v<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">Out&#91;<\/span><span style=\"color: #BF9EEE\">9<\/span><span style=\"color: #F6F6F4\">&#93;: array(&#91; <\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">,  <\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\">,  <\/span><span style=\"color: #BF9EEE\">4<\/span><span style=\"color: #F6F6F4\">,  <\/span><span style=\"color: #BF9EEE\">6<\/span><span style=\"color: #F6F6F4\">,  <\/span><span style=\"color: #BF9EEE\">8<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">10<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">12<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">14<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">16<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">18<\/span><span style=\"color: #F6F6F4\">&#93;)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In such cases, NumPy relies on highly optimized and performant C code as its computation engine. While the performance advantage is hardly noticeable for small datasets, it can become orders of magnitude on large datasets.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>In &#91;10&#93;: N = 10_000_000\n\nIn &#91;11&#93;: v = list(range(N))\n\nIn &#91;12&#93;: %timeit &#91;2 * i &lt;strong>for &lt;\/strong>i &lt;strong>in &lt;\/strong>v&#93;\n         313 ms \u00b1 10.4 ms per loop (mean \u00b1 std. dev. of 7 runs, 1 loop each)\n\nIn &#91;13&#93;: v = np.arange(N)\n\nIn &#91;14&#93;: %timeit 2 * v\n         7.99 ms \u00b1 86.5 \u00b5s per loop (mean \u00b1 std. dev. of 7 runs, 100 loops each)<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">10<\/span><span style=\"color: #F6F6F4\">&#93;: N <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">10_000_000<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">11<\/span><span style=\"color: #F6F6F4\">&#93;: v <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1; font-style: italic\">list<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(N))<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">12<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #F286C4\">%<\/span><span style=\"color: #F6F6F4\">timeit &#91;<\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #F6F6F4\"> i <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;for<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\">i <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\">v&#93;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">313<\/span><span style=\"color: #F6F6F4\"> ms \u00b1 <\/span><span style=\"color: #BF9EEE\">10.4<\/span><span style=\"color: #F6F6F4\"> ms per loop (mean \u00b1 std. dev. of <\/span><span style=\"color: #BF9EEE\">7<\/span><span style=\"color: #F6F6F4\"> runs, <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\"> loop each)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">13<\/span><span style=\"color: #F6F6F4\">&#93;: v <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> np.arange(N)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">14<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #F286C4\">%<\/span><span style=\"color: #F6F6F4\">timeit <\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #F6F6F4\"> v<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">7.99<\/span><span style=\"color: #F6F6F4\"> ms \u00b1 <\/span><span style=\"color: #BF9EEE\">86.5<\/span><span style=\"color: #F6F6F4\"> \u00b5s per loop (mean \u00b1 std. dev. of <\/span><span style=\"color: #BF9EEE\">7<\/span><span style=\"color: #F6F6F4\"> runs, <\/span><span style=\"color: #BF9EEE\">100<\/span><span style=\"color: #F6F6F4\"> loops each)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>While NumPy is important in its own right, it also builds the basis for many other important Python packages. One of them is pandas.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"pandas-package-for-financial-analysis\">Other useful python packages for financial analysis: How to use the Pandas Package<\/h2>\n\n\n\n<p>While NumPy has its strengths in efficient numerical operations, the pandas package (see <a href=\"https:\/\/pandas.pydata.org\/\" target=\"_blank\" rel=\"noopener\">https:\/\/pandas.pydata.org<\/a>) is strong when it comes to data processing and analysis. On its Webpage, you find the following description:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>pandas is a fast, powerful, flexible and easy to use open source data analysis and manipulation tool, built on top of the Python programming language.<\/p>\n<\/blockquote>\n\n\n\n<p>Pandas helps you manage the whole life cycle of financial data: retrieval, cleaning, processing, analysis, visualization, and storage. The following Python code illustrates this for end-of-day stock price data. The data is retrieved from a remote source, the data is cleaned, a simple moving average is added, and the result is visualized in figure <a href=\"https:\/\/certificate.tpq.io\/why.html#pandas\" target=\"_blank\" rel=\"noopener\">Stock price data for the Apple Stock and simple moving average (SMA)<\/a>. In this context, the code makes use of the major Python plotting package, matplotlib (see <a href=\"https:\/\/matplotlib.org\" target=\"_blank\" rel=\"noopener\">https:\/\/matplotlib.org<\/a>).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>In &#91;15&#93;: &lt;strong>import &lt;\/strong>pandas &lt;strong>as &lt;\/strong>pd\n         &lt;strong>from &lt;\/strong>pylab &lt;strong>import &lt;\/strong>plt\n         plt.style.use('seaborn')\n         %config InlineBackend.figure_format = 'svg'\n\nIn &#91;16&#93;: data = pd.read_csv('https:\/\/certificate.tpq.io\/mlfin.csv',\n                            index_col=0, parse_dates=True)\n         data = pd.DataFrame(data&#91;'AAPL.O'&#93;).dropna()\n\nIn &#91;17&#93;: data&#91;'SMA'&#93; = data.rolling(100).mean()\n\nIn &#91;18&#93;: data.tail()\nOut&#91;18&#93;:             AAPL.O       SMA\n         Date\n         2022-10-26  149.35  151.2670\n         2022-10-27  144.80  151.2536\n         2022-10-28  155.74  151.3239\n         2022-10-31  153.34  151.3777\n         2022-11-01  150.65  151.4578\n\nIn &#91;19&#93;: data.plot();\nOut[]: &lt;Figure size 576x396 &lt;strong>with &lt;\/strong>1 Axes>\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">15<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;import<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\">pandas <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;as<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\">pd<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;from<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\">pylab <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;import<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\">plt<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         plt.style.use(<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">seaborn<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #F286C4\">%<\/span><span style=\"color: #F6F6F4\">config InlineBackend.figure_format <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">svg<\/span><span style=\"color: #DEE492\">&#39;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">16<\/span><span style=\"color: #F6F6F4\">&#93;: data <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> pd.read_csv(<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">https:\/\/certificate.tpq.io\/mlfin.csv<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #F6F6F4\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">                            <\/span><span style=\"color: #FFB86C; font-style: italic\">index_col<\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #FFB86C; font-style: italic\">parse_dates<\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #BF9EEE\">True<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         data <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> pd.DataFrame(data&#91;<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">AAPL.O<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #F6F6F4\">&#93;).dropna()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">17<\/span><span style=\"color: #F6F6F4\">&#93;: data&#91;<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">SMA<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #F6F6F4\">&#93; <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> data.rolling(<\/span><span style=\"color: #BF9EEE\">100<\/span><span style=\"color: #F6F6F4\">).mean()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">18<\/span><span style=\"color: #F6F6F4\">&#93;: data.tail()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">Out&#91;<\/span><span style=\"color: #BF9EEE\">18<\/span><span style=\"color: #F6F6F4\">&#93;:             <\/span><span style=\"color: #BF9EEE\">AAPL<\/span><span style=\"color: #F6F6F4\">.O       <\/span><span style=\"color: #BF9EEE\">SMA<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         Date<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">2022<\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #BF9EEE\">10<\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #BF9EEE\">26<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">149.35<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">151.2670<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">2022<\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #BF9EEE\">10<\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #BF9EEE\">27<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">144.80<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">151.2536<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">2022<\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #BF9EEE\">10<\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #BF9EEE\">28<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">155.74<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">151.3239<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">2022<\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #BF9EEE\">10<\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #BF9EEE\">31<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">153.34<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">151.3777<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">2022<\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #BF9EEE\">11<\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #EE6666; font-style: italic; text-decoration: underline\">1<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">150.65<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">151.4578<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">19<\/span><span style=\"color: #F6F6F4\">&#93;: data.plot()<\/span><span style=\"color: #F6F6F4; font-style: italic; text-decoration: underline\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">Out[]: <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">Figure size <\/span><span style=\"color: #EE6666; font-style: italic; text-decoration: underline\">576x396<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;with<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\"> Axes<\/span><span style=\"color: #F286C4\">&gt;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"481\" src=\"https:\/\/beebole.com\/blog\/wp-content\/uploads\/2023\/03\/python-finance-example-700x481.png\" alt=\"Python is everywhere in finance.\" class=\"wp-image-10099\" title=\"\" srcset=\"https:\/\/beebole.com\/blog\/wp-content\/uploads\/2023\/03\/python-finance-example-700x481.png 700w, https:\/\/beebole.com\/blog\/wp-content\/uploads\/2023\/03\/python-finance-example-768x528.png 768w, https:\/\/beebole.com\/blog\/wp-content\/uploads\/2023\/03\/python-finance-example.png 800w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><figcaption class=\"wp-element-caption\">Stock price data for the Apple Stock and simple moving average (SMA)<\/figcaption><\/figure>\n\n\n\n<p>While pandas brings powerful data analysis capabilities, it also comes with most of the advantages of NumPy, such as vectorized operations. The following code creates a small two-dimensional DataFrame object and implements vectorized numerical operations on it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>In &#91;20&#93;: df = pd.DataFrame(np.arange(12).reshape(4, 3))\n         df\nOut&#91;20&#93;:    0   1   2\n         0  0   1   2\n         1  3   4   5\n         2  6   7   8\n         3  9  10  11\n\nIn &#91;21&#93;: 2 * df\nOut&#91;21&#93;:     0   1   2\n         0   0   2   4\n         1   6   8  10\n         2  12  14  16\n         3  18  20  22\n\nIn &#91;22&#93;: df ** 3 - df ** 2 + 1.5\nOut&#91;22&#93;:        0      1       2\n         0    1.5    1.5     5.5\n         1   19.5   49.5   101.5\n         2  181.5  295.5   449.5\n         3  649.5  901.5  1211.5\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">20<\/span><span style=\"color: #F6F6F4\">&#93;: df <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> pd.DataFrame(np.arange(<\/span><span style=\"color: #BF9EEE\">12<\/span><span style=\"color: #F6F6F4\">).reshape(<\/span><span style=\"color: #BF9EEE\">4<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #BF9EEE\">3<\/span><span style=\"color: #F6F6F4\">))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         df<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">Out&#91;<\/span><span style=\"color: #BF9EEE\">20<\/span><span style=\"color: #F6F6F4\">&#93;:    <\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">   <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">   <\/span><span style=\"color: #BF9EEE\">2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">   <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">   <\/span><span style=\"color: #BF9EEE\">2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">3<\/span><span style=\"color: #F6F6F4\">   <\/span><span style=\"color: #BF9EEE\">4<\/span><span style=\"color: #F6F6F4\">   <\/span><span style=\"color: #BF9EEE\">5<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">6<\/span><span style=\"color: #F6F6F4\">   <\/span><span style=\"color: #BF9EEE\">7<\/span><span style=\"color: #F6F6F4\">   <\/span><span style=\"color: #BF9EEE\">8<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">3<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">9<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">10<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">11<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">21<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">*<\/span><span style=\"color: #F6F6F4\"> df<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">Out&#91;<\/span><span style=\"color: #BF9EEE\">21<\/span><span style=\"color: #F6F6F4\">&#93;:     <\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">   <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">   <\/span><span style=\"color: #BF9EEE\">2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">   <\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">   <\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\">   <\/span><span style=\"color: #BF9EEE\">4<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">   <\/span><span style=\"color: #BF9EEE\">6<\/span><span style=\"color: #F6F6F4\">   <\/span><span style=\"color: #BF9EEE\">8<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">10<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">12<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">14<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">16<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">3<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">18<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">20<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">22<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">22<\/span><span style=\"color: #F6F6F4\">&#93;: df <\/span><span style=\"color: #F286C4\">**<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">3<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> df <\/span><span style=\"color: #F286C4\">**<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1.5<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">Out&#91;<\/span><span style=\"color: #BF9EEE\">22<\/span><span style=\"color: #F6F6F4\">&#93;:        <\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">      <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">       <\/span><span style=\"color: #BF9EEE\">2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #BF9EEE\">1.5<\/span><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #BF9EEE\">1.5<\/span><span style=\"color: #F6F6F4\">     <\/span><span style=\"color: #BF9EEE\">5.5<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">   <\/span><span style=\"color: #BF9EEE\">19.5<\/span><span style=\"color: #F6F6F4\">   <\/span><span style=\"color: #BF9EEE\">49.5<\/span><span style=\"color: #F6F6F4\">   <\/span><span style=\"color: #BF9EEE\">101.5<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">181.5<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">295.5<\/span><span style=\"color: #F6F6F4\">   <\/span><span style=\"color: #BF9EEE\">449.5<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">3<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">649.5<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">901.5<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">1211.5<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"performance\">Performance<\/h2>\n\n\n\n<p>NumPy and pandas, with their vectorized operations, are already quite fast and in particular faster than pure Python code. However, the vectorization of code is not always the best option or even a feasible option to speed up the execution of Python code. Consider something as simple as the calculation of the sum of all elements of a larger vector. The following Python code implements the summation in pure Python. The memory footprint of the v = range(N) object is negligible with 48 bytes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>In &#91;23&#93;: import sys\n\nIn &#91;24&#93;: N = 10_000_000\n\nIn &#91;25&#93;: v = range(N)\n\nIn &#91;26&#93;: sum(v)\nOut&#91;26&#93;: 49999995000000\n\nIn &#91;27&#93;: %timeit sum(v)\n         103 ms \u00b1 110 \u00b5s per loop (mean \u00b1 std. dev. of 7 runs, 10 loops each)\n\nIn &#91;28&#93;: sys.getsizeof(v)\nOut&#91;28&#93;: 48\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">23<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #F286C4\">import<\/span><span style=\"color: #F6F6F4\"> sys<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">24<\/span><span style=\"color: #F6F6F4\">&#93;: N <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">10_000_000<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">25<\/span><span style=\"color: #F6F6F4\">&#93;: v <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(N)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">26<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #97E1F1\">sum<\/span><span style=\"color: #F6F6F4\">(v)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">Out&#91;<\/span><span style=\"color: #BF9EEE\">26<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #BF9EEE\">49999995000000<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">27<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #F286C4\">%<\/span><span style=\"color: #F6F6F4\">timeit <\/span><span style=\"color: #97E1F1\">sum<\/span><span style=\"color: #F6F6F4\">(v)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">103<\/span><span style=\"color: #F6F6F4\"> ms \u00b1 <\/span><span style=\"color: #BF9EEE\">110<\/span><span style=\"color: #F6F6F4\"> \u00b5s per loop (mean \u00b1 std. dev. of <\/span><span style=\"color: #BF9EEE\">7<\/span><span style=\"color: #F6F6F4\"> runs, <\/span><span style=\"color: #BF9EEE\">10<\/span><span style=\"color: #F6F6F4\"> loops each)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">28<\/span><span style=\"color: #F6F6F4\">&#93;: sys.getsizeof(v)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">Out&#91;<\/span><span style=\"color: #BF9EEE\">28<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #BF9EEE\">48<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>The implementation of the same task with NumPy in the following code speeds up the code execution considerably. However, the memory footprint increases from 48 bytes to 80 megabytes. In many scenarios, this might be prohibitive.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>In &#91;29&#93;: v = np.arange(N)\n\nIn &#91;30&#93;: np.sum(v)\nOut&#91;30&#93;: 49999995000000\n\nIn &#91;31&#93;: %timeit np.sum(v)\n         3.79 ms \u00b1 7.63 \u00b5s per loop (mean \u00b1 std. dev. of 7 runs, 100 loops each)\n\nIn &#91;32&#93;: sys.getsizeof(v)\nOut&#91;32&#93;: 80000112\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">29<\/span><span style=\"color: #F6F6F4\">&#93;: v <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> np.arange(N)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">30<\/span><span style=\"color: #F6F6F4\">&#93;: np.sum(v)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">Out&#91;<\/span><span style=\"color: #BF9EEE\">30<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #BF9EEE\">49999995000000<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">31<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #F286C4\">%<\/span><span style=\"color: #F6F6F4\">timeit np.sum(v)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">3.79<\/span><span style=\"color: #F6F6F4\"> ms \u00b1 <\/span><span style=\"color: #BF9EEE\">7.63<\/span><span style=\"color: #F6F6F4\"> \u00b5s per loop (mean \u00b1 std. dev. of <\/span><span style=\"color: #BF9EEE\">7<\/span><span style=\"color: #F6F6F4\"> runs, <\/span><span style=\"color: #BF9EEE\">100<\/span><span style=\"color: #F6F6F4\"> loops each)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">32<\/span><span style=\"color: #F6F6F4\">&#93;: sys.getsizeof(v)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">Out&#91;<\/span><span style=\"color: #BF9EEE\">32<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #BF9EEE\">80000112<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>One option to increase the execution speed while preserving memory efficiency is the use of dynamic compiling techniques. numba is a Python package that allows the dynamic compilation of pure Python code or code that is a mixture of NumPy and pure Python. The following code first implements the summation as a regular Python function. It then dynamically compiles the function with numba. The performance of the numba compiled version is orders of magnitude faster than the Python version. It also preserves the memory efficiency of the Python version.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>In &#91;33&#93;: &lt;strong>import &lt;\/strong>numba\n\nIn &#91;34&#93;: &lt;strong>def &lt;\/strong>sum_py(N):\n             s = 0\n             &lt;strong>for &lt;\/strong>i &lt;strong>in &lt;\/strong>range(N):\n                 s += i\n             &lt;strong>return &lt;\/strong>s\n\nIn &#91;35&#93;: sum_py(N)\nOut&#91;35&#93;: 49999995000000\n\nIn &#91;36&#93;: sum_nb = numba.jit(sum_py)\n\nIn &#91;37&#93;: sum_nb(N)\nOut&#91;37&#93;: 49999995000000\n\nIn &#91;38&#93;: %timeit sum_nb(N)\n         87.9 ns \u00b1 0.319 ns per loop (mean \u00b1 std. dev. of 7 runs, 10,000,000 loops\n          each)\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">33<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;import<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\">numba<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">34<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;def<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\">sum_py(N):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">             s <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">             <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;for<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\">i <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;in<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #97E1F1\">range<\/span><span style=\"color: #F6F6F4\">(N):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">                 s <\/span><span style=\"color: #F286C4\">+=<\/span><span style=\"color: #F6F6F4\"> i<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">             <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;return<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\">s<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">35<\/span><span style=\"color: #F6F6F4\">&#93;: sum_py(N)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">Out&#91;<\/span><span style=\"color: #BF9EEE\">35<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #BF9EEE\">49999995000000<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">36<\/span><span style=\"color: #F6F6F4\">&#93;: sum_nb <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> numba.jit(sum_py)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">37<\/span><span style=\"color: #F6F6F4\">&#93;: sum_nb(N)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">Out&#91;<\/span><span style=\"color: #BF9EEE\">37<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #BF9EEE\">49999995000000<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">38<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #F286C4\">%<\/span><span style=\"color: #F6F6F4\">timeit sum_nb(N)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">87.9<\/span><span style=\"color: #F6F6F4\"> ns \u00b1 <\/span><span style=\"color: #BF9EEE\">0.319<\/span><span style=\"color: #F6F6F4\"> ns per loop (mean \u00b1 std. dev. of <\/span><span style=\"color: #BF9EEE\">7<\/span><span style=\"color: #F6F6F4\"> runs, <\/span><span style=\"color: #BF9EEE\">10<\/span><span style=\"color: #F6F6F4\">,<\/span><span style=\"color: #BF9EEE\">000<\/span><span style=\"color: #F6F6F4\">,<\/span><span style=\"color: #BF9EEE\">000<\/span><span style=\"color: #F6F6F4\"> loops<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">          each)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Overall, it is safe to say that Python, by applying the right idioms and techniques, is fast enough even for computationally demanding financial algorithms.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"input-output-operations-for-finance\">Input-output operations<\/h2>\n\n\n\n<p><strong>Execution speed is of the essence in many financial applications<\/strong>. However, reading and writing large data sets often proves to be a bottleneck as well. Fortunately, there are a few options in terms of storage technologies that allow for really fast IO operations. <\/p>\n\n\n\n<p>Consider the following Python code that generates by the use of NumPy a larger sample data set with pseudo-random numbers. The data set is written to disk as a CSV file. This is a relatively slow operation because CSV files are simple text-based files. Similarly, reading the data back from the CSV file is also relatively slow. Nevertheless, CSV is still a standard data exchange format in the financial industry and elsewhere.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>In &#91;39&#93;: &lt;strong>from &lt;\/strong>numpy.random &lt;strong>import &lt;\/strong>default_rng\n\nIn &#91;40&#93;: rng = default_rng()\n\nIn &#91;41&#93;: N = 10_000_000\n\nIn &#91;42&#93;: df = pd.DataFrame(rng.random((N, 2)))\n\nIn &#91;43&#93;: fn = '\/Users\/yves\/Temp\/data\/data.csv'\n\nIn &#91;44&#93;: %time df.to_csv(fn)\n         CPU times: user 18.6 s, sys: 542 ms, total: 19.1 s\n         Wall time: 19.3 s\n\nIn &#91;45&#93;: %time new = pd.read_csv(fn, index_col=0, parse_dates=True)\n         CPU times: user 5.54 s, sys: 623 ms, total: 6.17 s\n         Wall time: 6.28 s<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">39<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;from<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\">numpy.random <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;import<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\">default_rng<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">40<\/span><span style=\"color: #F6F6F4\">&#93;: rng <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> default_rng()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">41<\/span><span style=\"color: #F6F6F4\">&#93;: N <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">10_000_000<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">42<\/span><span style=\"color: #F6F6F4\">&#93;: df <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> pd.DataFrame(rng.random((N, <\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\">)))<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">43<\/span><span style=\"color: #F6F6F4\">&#93;: fn <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">\/Users\/yves\/Temp\/data\/data.csv<\/span><span style=\"color: #DEE492\">&#39;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">44<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #F286C4\">%<\/span><span style=\"color: #F6F6F4\">time df.to_csv(fn)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">CPU<\/span><span style=\"color: #F6F6F4\"> times: user <\/span><span style=\"color: #BF9EEE\">18.6<\/span><span style=\"color: #F6F6F4\"> s, sys: <\/span><span style=\"color: #BF9EEE\">542<\/span><span style=\"color: #F6F6F4\"> ms, total: <\/span><span style=\"color: #BF9EEE\">19.1<\/span><span style=\"color: #F6F6F4\"> s<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         Wall time: <\/span><span style=\"color: #BF9EEE\">19.3<\/span><span style=\"color: #F6F6F4\"> s<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">45<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #F286C4\">%<\/span><span style=\"color: #F6F6F4\">time new <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> pd.read_csv(fn, <\/span><span style=\"color: #FFB86C; font-style: italic\">index_col<\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #FFB86C; font-style: italic\">parse_dates<\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #BF9EEE\">True<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">CPU<\/span><span style=\"color: #F6F6F4\"> times: user <\/span><span style=\"color: #BF9EEE\">5.54<\/span><span style=\"color: #F6F6F4\"> s, sys: <\/span><span style=\"color: #BF9EEE\">623<\/span><span style=\"color: #F6F6F4\"> ms, total: <\/span><span style=\"color: #BF9EEE\">6.17<\/span><span style=\"color: #F6F6F4\"> s<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         Wall time: <\/span><span style=\"color: #BF9EEE\">6.28<\/span><span style=\"color: #F6F6F4\"> s<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>On the other hand, relying on a binary storage technology such as HDF5 (see <a href=\"https:\/\/hdfgroup.org\/\" target=\"_blank\" rel=\"noopener\">https:\/\/hdfgroup.org<\/a>) speeds up things considerably. Both, writing the data to disk and reading the data back from disk, are orders of magnitude faster in this case. With technologies such as HDF5, IO operations are often only bound by the speed of the available hardware.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>In &#91;46&#93;: fn = '\/Users\/yves\/Temp\/data\/data.hd5'\n\nIn &#91;47&#93;: %time df.to_hdf(fn, 'data')\n         CPU times: user 25.1 ms, sys: 83.8 ms, total: 109 ms\n         Wall time: 327 ms\n\nIn &#91;48&#93;: %time new = pd.read_hdf(fn)\n         CPU times: user 18.5 ms, sys: 67.1 ms, total: 85.7 ms\n         Wall time: 93.3 ms<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">46<\/span><span style=\"color: #F6F6F4\">&#93;: fn <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">\/Users\/yves\/Temp\/data\/data.hd5<\/span><span style=\"color: #DEE492\">&#39;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">47<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #F286C4\">%<\/span><span style=\"color: #F6F6F4\">time df.to_hdf(fn, <\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">data<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">CPU<\/span><span style=\"color: #F6F6F4\"> times: user <\/span><span style=\"color: #BF9EEE\">25.1<\/span><span style=\"color: #F6F6F4\"> ms, sys: <\/span><span style=\"color: #BF9EEE\">83.8<\/span><span style=\"color: #F6F6F4\"> ms, total: <\/span><span style=\"color: #BF9EEE\">109<\/span><span style=\"color: #F6F6F4\"> ms<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         Wall time: <\/span><span style=\"color: #BF9EEE\">327<\/span><span style=\"color: #F6F6F4\"> ms<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">48<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #F286C4\">%<\/span><span style=\"color: #F6F6F4\">time new <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> pd.read_hdf(fn)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">CPU<\/span><span style=\"color: #F6F6F4\"> times: user <\/span><span style=\"color: #BF9EEE\">18.5<\/span><span style=\"color: #F6F6F4\"> ms, sys: <\/span><span style=\"color: #BF9EEE\">67.1<\/span><span style=\"color: #F6F6F4\"> ms, total: <\/span><span style=\"color: #BF9EEE\">85.7<\/span><span style=\"color: #F6F6F4\"> ms<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         Wall time: <\/span><span style=\"color: #BF9EEE\">93.3<\/span><span style=\"color: #F6F6F4\"> ms<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>If speed is not of the essence, but rather the storage in a more widely used format\u2009\u2014\u2009such as in an SQL relational database\u2009\u2014\u2009pandas can also help. The following Python code writes the data set to an SQLite relational database and reads the data back into memory. The speed in this case is much lower as compared to the HDF5-based storage. Overall, the HDF5 option is also the most efficient in terms of file size on disk.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>In &#91;49&#93;: &lt;strong>import &lt;\/strong>sqlite3 &lt;strong>as &lt;\/strong>sq3\n\nIn &#91;50&#93;: fn = '\/Users\/yves\/Temp\/data\/data.sq3'\n\nIn &#91;51&#93;: con = sq3.connect(fn)\n\nIn &#91;52&#93;: %time df.to_sql('data', con)\n         CPU times: user 7.79 s, sys: 1.54 s, total: 9.33 s\n         Wall time: 10.1 s\n\nOut&#91;52&#93;: 10000000\n\nIn &#91;53&#93;: %time new = pd.read_sql('SELECT * FROM data', con)\n         CPU times: user 6.08 s, sys: 3.59 s, total: 9.67 s\n         Wall time: 11.4 s\n\nIn &#91;54&#93;: ls -n \/Users\/yves\/Temp\/data\n         total 2272264\n         -rw-r--r--  1 501  20  464285269 Dec 19 14:30 data.csv\n         -rw-r--r--  1 501  20  240007240 Dec 19 14:30 data.hd5\n         -rw-r--r--  1 501  20  443166720 Dec 19 14:30 data.sq3\n\nIn &#91;55&#93;: rm \/Users\/yves\/Temp\/data\/data.*\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">49<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;import<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\">sqlite3 <\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;as<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;\/<\/span><span style=\"color: #F6F6F4\">strong<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\">sq3<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">50<\/span><span style=\"color: #F6F6F4\">&#93;: fn <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">\/Users\/yves\/Temp\/data\/data.sq3<\/span><span style=\"color: #DEE492\">&#39;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">51<\/span><span style=\"color: #F6F6F4\">&#93;: con <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> sq3.connect(fn)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">52<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #F286C4\">%<\/span><span style=\"color: #F6F6F4\">time df.to_sql(<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">data<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #F6F6F4\">, con)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">CPU<\/span><span style=\"color: #F6F6F4\"> times: user <\/span><span style=\"color: #BF9EEE\">7.79<\/span><span style=\"color: #F6F6F4\"> s, sys: <\/span><span style=\"color: #BF9EEE\">1.54<\/span><span style=\"color: #F6F6F4\"> s, total: <\/span><span style=\"color: #BF9EEE\">9.33<\/span><span style=\"color: #F6F6F4\"> s<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         Wall time: <\/span><span style=\"color: #BF9EEE\">10.1<\/span><span style=\"color: #F6F6F4\"> s<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">Out&#91;<\/span><span style=\"color: #BF9EEE\">52<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #BF9EEE\">10000000<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">53<\/span><span style=\"color: #F6F6F4\">&#93;: <\/span><span style=\"color: #F286C4\">%<\/span><span style=\"color: #F6F6F4\">time new <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> pd.read_sql(<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #E7EE98\">SELECT * FROM data<\/span><span style=\"color: #DEE492\">&#39;<\/span><span style=\"color: #F6F6F4\">, con)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #BF9EEE\">CPU<\/span><span style=\"color: #F6F6F4\"> times: user <\/span><span style=\"color: #BF9EEE\">6.08<\/span><span style=\"color: #F6F6F4\"> s, sys: <\/span><span style=\"color: #BF9EEE\">3.59<\/span><span style=\"color: #F6F6F4\"> s, total: <\/span><span style=\"color: #BF9EEE\">9.67<\/span><span style=\"color: #F6F6F4\"> s<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         Wall time: <\/span><span style=\"color: #BF9EEE\">11.4<\/span><span style=\"color: #F6F6F4\"> s<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">54<\/span><span style=\"color: #F6F6F4\">&#93;: ls <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\">n <\/span><span style=\"color: #F286C4\">\/<\/span><span style=\"color: #F6F6F4\">Users<\/span><span style=\"color: #F286C4\">\/<\/span><span style=\"color: #F6F6F4\">yves<\/span><span style=\"color: #F286C4\">\/<\/span><span style=\"color: #F6F6F4\">Temp<\/span><span style=\"color: #F286C4\">\/<\/span><span style=\"color: #F6F6F4\">data<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         total <\/span><span style=\"color: #BF9EEE\">2272264<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\">rw<\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\">r<\/span><span style=\"color: #EE6666; font-style: italic; text-decoration: underline\">--<\/span><span style=\"color: #F6F6F4\">r<\/span><span style=\"color: #EE6666; font-style: italic; text-decoration: underline\">--<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">501<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">20<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">464285269<\/span><span style=\"color: #F6F6F4\"> Dec <\/span><span style=\"color: #BF9EEE\">19<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">14<\/span><span style=\"color: #F6F6F4\">:<\/span><span style=\"color: #BF9EEE\">30<\/span><span style=\"color: #F6F6F4\"> data.csv<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\">rw<\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\">r<\/span><span style=\"color: #EE6666; font-style: italic; text-decoration: underline\">--<\/span><span style=\"color: #F6F6F4\">r<\/span><span style=\"color: #EE6666; font-style: italic; text-decoration: underline\">--<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">501<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">20<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">240007240<\/span><span style=\"color: #F6F6F4\"> Dec <\/span><span style=\"color: #BF9EEE\">19<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">14<\/span><span style=\"color: #F6F6F4\">:<\/span><span style=\"color: #BF9EEE\">30<\/span><span style=\"color: #F6F6F4\"> data.hd5<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">         <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\">rw<\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\">r<\/span><span style=\"color: #EE6666; font-style: italic; text-decoration: underline\">--<\/span><span style=\"color: #F6F6F4\">r<\/span><span style=\"color: #EE6666; font-style: italic; text-decoration: underline\">--<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">501<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">20<\/span><span style=\"color: #F6F6F4\">  <\/span><span style=\"color: #BF9EEE\">443166720<\/span><span style=\"color: #F6F6F4\"> Dec <\/span><span style=\"color: #BF9EEE\">19<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">14<\/span><span style=\"color: #F6F6F4\">:<\/span><span style=\"color: #BF9EEE\">30<\/span><span style=\"color: #F6F6F4\"> data.sq3<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">In &#91;<\/span><span style=\"color: #BF9EEE\">55<\/span><span style=\"color: #F6F6F4\">&#93;: rm <\/span><span style=\"color: #F286C4\">\/<\/span><span style=\"color: #F6F6F4\">Users<\/span><span style=\"color: #F286C4\">\/<\/span><span style=\"color: #F6F6F4\">yves<\/span><span style=\"color: #F286C4\">\/<\/span><span style=\"color: #F6F6F4\">Temp<\/span><span style=\"color: #F286C4\">\/<\/span><span style=\"color: #F6F6F4\">data<\/span><span style=\"color: #F286C4\">\/<\/span><span style=\"color: #F6F6F4\">data.<\/span><span style=\"color: #F286C4\">*<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"can-Python-be-used-with-powerbi\">Can Python be used with Microsoft PowerBI?<\/h2>\n\n\n\n<p>Yes, Python can indeed be used with Power BI, a business analytics tool developed by Microsoft. Power BI provides interactive visualizations and business intelligence capabilities, for instance for <a href=\"https:\/\/beebole.com\/blog\/power-bi-for-planning-budgeting-and-forecasting\/\" data-type=\"post\" data-id=\"9926\">planning, budgeting and forecasting<\/a>. The tool allows for Python integration in a few different ways:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Python Scripts as a data source:<\/strong> Power BI Desktop supports the use of Python scripts as a data source. You can run a Python script directly in Power BI and the output of that script, typically a dataframe, can be imported directly into Power BI as a dataset.<\/li>\n\n\n\n<li><strong>Python Visuals:<\/strong> Power BI allows you to create your own visuals using Python. In the visuals pane, you can select Python visuals and write Python code that generates a plot using libraries like matplotlib, seaborn, or plotly. The visual updates live as you manipulate the data or adjust the code.<\/li>\n\n\n\n<li><strong>Running Python scripts in Power Query:<\/strong> If you&#8217;re using Power Query to clean and transform your data, you can also run Python scripts. This is useful for leveraging Python&#8217;s powerful data manipulation libraries like pandas within your Power Query workflows.<\/li>\n<\/ol>\n\n\n\n<p>To use Python in Power BI, you need to install Python on your machine, and then specify the installation path in the Power BI options. You also need to install any Python libraries that you plan to use. Remember to verify if any newer Python versions or more specific functionalities have been added since then.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"learning-python-for-finance\">Get your next belt in Python for finance<\/h2>\n\n\n\n<p>If you are interested in entering the financial industry or accelerating your career in it, there is hardly any better option than to learn Python programming and advance your level from either white to green belt or from green belt to brown belt. Like English as a spoken language and mathematics as a symbolic language, Python can be considered universal in its reach, scope, and applicability. This not only holds true for finance but for other domains as well. It also holds true for such <a href=\"https:\/\/beebole.com\/blog\/reskilling-finance-teams-business-intelligence\/\">fundamental technologies as machine learning and artificial intelligence<\/a>. Python is indeed everywhere these days and picking it up might be one of the best options to turbo-charge your career.<\/p>\n\n\n\n<div  class=\"mx-auto bbl_cta_block bk-light\">\n\t<a class=\"bbl_cta_block-blockcontent bbl_cta_block-link d-block overflow-hidden position-relative rounded-4 text-decoration-none\" href=\"https:\/\/beebole.com\/blog\/technology-trends-for-cfos\" title=\"7 top technology trends for CFOs that will shape the future: from Artificial Intelligence (AI) to strategic partnerships [Experts weigh in]\">\n\t\t\t\t\t<div class=\"bbl-blue-dot object-fit-cover position-absolute start-0 top-0\" style=\"background-image: url(https:\/\/beebole.com\/blog\/wp-content\/themes\/sage\/public\/images\/blue-dot.a385a5.svg)\"><\/div>\n\t\t\t\t<div class=\"bottom-0 end-0 object-fit-cover position-absolute bbl-orange-dot\" style=\"background-image: url(https:\/\/beebole.com\/blog\/wp-content\/themes\/sage\/public\/images\/orange-dot.47ecad.svg)\"><\/div>\n\n\t\t<div class=\"bbl_cta_block-row align-items-center d-flex flex-md-row justify-content-center mx-0 no-gutters position-relative row\">\n\t\t\t<div class=\"bbl_cta_block-img-col col d-flex justify-content-start pe-md-2 pe-lg-4 px-0\">\n\t\t\t\t<img\n\t\t\t\t\talt=\"7 top technology trends for CFOs that will shape the future: from Artificial Intelligence (AI) to strategic partnerships [Experts weigh in]\"\n\t\t\t\t\tclass=\"d-block h-auto mw-lg-100\"\n\t\t\t\t\tloading=\"lazy\"\n\t\t\t\t\theight=\"240\"\n\t\t\t\t\tsrc=\"https:\/\/beebole.com\/blog\/wp-content\/themes\/sage\/public\/images\/promotion-post.9422b6.png\"\n\t\t\t\t\twidth=\"360\"\n\t\t\t\t\/>\n\t\t\t<\/div>\n\t\t\t<div class=\"bbl_cta_block-text-col col mt-md-0 ps-0\">\n\t\t\t\t\t\t\t\t\t<div class=\"mb-1\"><div class=\"bbl_cta_block-label lh-base mb-2 mb-md-4\">RELATED POST<\/div><\/div>\n\t\t\t\t\t\t\t\t<div class=\"bbl_cta_block-title lh-base\">7 top technology trends for CFOs that will shape the future: from Artificial Intelligence (AI) to strategic partnerships [Experts weigh in]<\/div>\n\t\t\t\t\t\t\t\t\t<div>\n\t\t\t\t\t\t<div class=\"bbl_cta_block-button h6 lh-1 mb-0 mt-3\">\n\t\t\t\t\t\t\tRead more\t\t\t\t\t\t\t<svg class=\"ms-2\" width=\"15\" height=\"14\" viewBox=\"0 0 15 14\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n\t\t\t\t\t\t\t\t<path d=\"M5.9375 1.09375L6.625 0.40625C6.9375 0.125 7.40625 0.125 7.6875 0.40625L13.7812 6.46875C14.0625 6.78125 14.0625 7.25 13.7812 7.53125L7.6875 13.625C7.40625 13.9062 6.9375 13.9062 6.625 13.625L5.9375 12.9375C5.65625 12.625 5.65625 12.1562 5.9375 11.8438L9.71875 8.25H0.75C0.3125 8.25 0 7.9375 0 7.5V6.5C0 6.09375 0.3125 5.75 0.75 5.75H9.71875L5.9375 2.1875C5.65625 1.875 5.625 1.40625 5.9375 1.09375Z\" fill=\"#313358\" \/>\n\t\t\t\t\t\t\t<\/svg>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t<\/a>\n<\/div>\n\n\n<p>\u2014<br>Photo by Chris Ried on Unsplash<\/p>\n<div class=\"bbl-post-disclaimer\">The experts who have written or contributed to this article are independent from Beebole, and their contribution doesn't serve as endorsement for our company\/tool or their past\/present organizations, employers, or associates.<\/div>","protected":false},"excerpt":{"rendered":"<p>Are you exploring the intriguing world of python for finance? Whether you aspire to work in the financial industry, or wish to advance your existing career, mastering Python is a key step forward. In today&#8217;s fast-paced financial arena, Python programming is a crucial skill that top institutions seek in their professionals. Dive into this article [&hellip;]<\/p>\n","protected":false},"author":45,"featured_media":11318,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4011],"tags":[1468,3980,4012],"class_list":["post-78","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-project-management","tag-finance","tag-reporting","tag-tutorials"],"acf":[],"_links":{"self":[{"href":"https:\/\/beebole.com\/blog\/wp-json\/wp\/v2\/posts\/78","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/beebole.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/beebole.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/beebole.com\/blog\/wp-json\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/beebole.com\/blog\/wp-json\/wp\/v2\/comments?post=78"}],"version-history":[{"count":16,"href":"https:\/\/beebole.com\/blog\/wp-json\/wp\/v2\/posts\/78\/revisions"}],"predecessor-version":[{"id":14247,"href":"https:\/\/beebole.com\/blog\/wp-json\/wp\/v2\/posts\/78\/revisions\/14247"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/beebole.com\/blog\/wp-json\/wp\/v2\/media\/11318"}],"wp:attachment":[{"href":"https:\/\/beebole.com\/blog\/wp-json\/wp\/v2\/media?parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/beebole.com\/blog\/wp-json\/wp\/v2\/categories?post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/beebole.com\/blog\/wp-json\/wp\/v2\/tags?post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}