3

I don't understand why I can't post my question on Stack Overflow, but I can post it here.

The text is exactly the same, but when I want to post it on Stack Overflow I can't do it because a message tells me that my code is not formatted properly. Yet it is the same text word for word (which I attach below). Do you know where the problem may come?

The following is exactly the post I would like to publish on Stack Overflow:

I am trying to build a quadratic model to study the evolution of the world population. I would like to display the evolution of the world population until 2100 using the quadratic model.

I took my data from this site: Worldometers. I downloaded the page and read it with Pandas.

Here is my code:

# Import of modules
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from types import SimpleNamespace

# Reading the file
Data = 'World Population by Year - Worldometer.html'
Data2 = pd.read_html(Data, header = 0, index_col = 0, decimal = 'M')

Data3 = Data2[0] # We extract a DataFrame from the file

Given the year jumps and the fact that the data may not have been reliable prior to 1951, I removed all data prior to 1951 to work only on data from 1951 to 2020.

# Delete the years before 1951
data = Data3.drop(Data.index[70:], axis = 0)

# Rename the names of the columns
data.index.names = ['Year'] # Rename the index
data.columns = ['Population', 'Annual change', 'Net change', 'Density (P/Km²)', 'Urban population', 'Urban population (%)'] # Renaming of other columns

population = data['Population']

total_growth = population[2020] - population[1951]

start = population.index[-1] # 1st year of data collected
current = population.index[0] # Last year of data collected
duration = current - start # Duration of the data collection period

start_population = population[start]
population_2020 = population[current]
difference_population = population_2020 - start_population

annual_growth = total_growth / duration

parameters = SimpleNamespace(start = start, end = current, start_population = start_population, annual_growth = annual_growth)

def model(parameters, growth_function):
    result = dict()
    result[parameters.start] = parameters.start_population
   
    for t in range(parameters.start, parameters.end):
        evolution = growth_function(t, result[t], parameters)
        result[t + 1] = result[t] + evolution
       
    # We convert the dictionary into a DataFrame in order to plot it
    result_items = result.items() # To obtain an object of type set with the keys  and the values of the dictionary
    result_list = list(result_items) # We then convert the set object into a list
    results = pd.DataFrame(result_list) # Finally we create a >DataFrame from a list
   
   results.columns = ['Year', 'World population'] # Name of the >columns of the DataFrame
   
   return results

parameters.alpha = parameters.birth_rate - parameters.death_rate

def quadratic(t, population, parameters):
    """Returns the quadratic function"""
    return parameters.alpha * population + parameters.beta * population ** 2

Parameters = SimpleNamespace(start = start, start_population = start_population, alpha = 25 / 1000, beta = -1.8 / 1000, end = 2100)

projection = model(Parameters, quadratic)
projection

Here is the result I get by running this code:

Year World Population
0 1951 2.584034e+09
1 1952 -1.201902e+16
2 1953 -2.600222e+29
3 1954 -1.217008e+56
4 1955 -2.665994e+109
... ... ...
145 2096 -inf
146 2097 -inf
147 2098 -inf
148 2099 -inf
149 2100 -inf

As you can see, I get totally inconsistent results from the first year calculated (i.e. 1952) and I don't know where this dysfunction can come from. Moreover, I get a RuntimeWarning: overflow encountered in double_scalars warning when I try to display the results of the simulation even if this does not prevent the result from being displayed.

No matter how hard I look, I can't figure out what's wrong with my code and my initial values. Where do you think these two problems come from?

12
  • 4
    The text can't be exactly the same, as you have the additional part that starts it's the same. Also you didn't say what happens when you do try to post your question. Are you, perhaps, question banned?
    – Thom A
    Apr 17, 2022 at 19:49
  • I copied and pasted the exact same text on meta Stack Overflow and realized that I could publish my post. So I added the paragraph above to know the reason why I could not publish my post on SO but I can publish it here. No I don't think I'm banned from questioning. At least nothing tells me so and I get the following error as stated in the first paragraph of this post: "Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon."
    – Donny
    Apr 17, 2022 at 19:54
  • If you use non-floating point numbers in the table, does the problem persist? The source data doesn't contain floating point numbers, so why convert them to a base 2 value and lose accuracy?
    – Thom A
    Apr 17, 2022 at 20:06
  • 1
    Are you quoting someone / some docs? if not, paragraphs don't start with >
    – rene
    Apr 17, 2022 at 20:08
  • @Larnu Yes unfortunately the problem persists when I use non-floating point numbers in the table. It is not the source data but the output I get after running my code
    – Donny
    Apr 17, 2022 at 20:11
  • @rene The paragraphs starting with > highlight the post I originally intended to publish on Stack Overflow. I added them last to distinguish this post from the post I wanted to publish on Stack Overflow. In any case even without > in front of the paragraphs I could publish my post here
    – Donny
    Apr 17, 2022 at 20:14
  • can you get rid of the table all together and see if the error persists? Can you remove this line from the prior to last paragraph: RuntimeWarning: overflow encountered in double_scalars, including the quotes and see if the error persists? You do have a blank line everywhere between paragraphs and tables and codeblocks, right?
    – rene
    Apr 17, 2022 at 20:17
  • Indeed when I get rid of the table and the warning line I can publish my post. I had skipped some lines between the paragraphs, the code blocks and the table. So what's wrong? I can't afford to publish a question without the table or the warning message at the risk of getting downvoted
    – Donny
    Apr 17, 2022 at 20:26
  • It is the table that is the problem. But I can't figure out why because I think I respect all the Stack Overflow editing rules
    – Donny
    Apr 17, 2022 at 20:47
  • 2
    Because values like -1.201902e+16 (and possibly -inf) will likely be seen as code, @Donny; hence why I suggested not putting them as floating point numbers. You could, instead, put them in inline code markdown; that might work.
    – Thom A
    Apr 17, 2022 at 21:15
  • 3
    Re: title, because there's no code quality check on meta.
    – Andrew T.
    Apr 18, 2022 at 1:19
  • 1
    The indentation of the Python code is incorrect near "results.columns" (8 instead of 9 (7 instead of 8 in the actual source code)). All the code is also indented by one space. There are 92 trailing spaces in the code part. Apr 19, 2022 at 4:54

0

Browse other questions tagged .