problem assigning values to pandas dataframe at specific location

Viewed 375

I have a problem assigning values to a dataframe at a specific location (column and index). First I create an empty dataframe:

self.timeseries = pd.DataFrame(
                columns = ["temperature", "state_of_charge", "m_ice", "m_water"],
                index = pd.date_range(
                        start = self.environment.start,
                        end = self.environment.end,
                        freq = self.environment.time_freq,
                        name = "time"
                        )
                )

Later I try to assign values to each column for each index:

        self.timeseries.temperature.loc[timestamp] = self.current_temp
        self.timeseries.state_of_charge.loc[timestamp] = self.state_of_charge
        self.timeseries.m_ice.loc[timestamp] = self.m_ice /self.mass
        self.timeseries.m_water.loc[timestamp] = self.m_water / self.mass

the first line of the code above seems to work. But once it reaches the second line, I get the following error message:

ValueError: No axis named 1 for object type <class 'pandas.core.series.Series'>

In other parts of the code exactly the same procedure is used, and it works.

Many thanks for each help in advance!

P.S.: I dont know, if it is necessary, but additional info:

I work with spyder 3.3.6 (Python 3.7) in anaconda 1.9.12

2 Answers
import pandas as pd
timeseries = pd.DataFrame(
                columns = ["temperature", "state_of_charge", "m_ice", "m_water"],
                index =pd.date_range(start ='01-01-2018',
                         end ='01-02-2018', freq ='5H')
                )
print(timeseries)
timeseries.temperature.loc['2018-01-01 00:00:00'] = 15
timeseries.state_of_charge.loc['2018-01-01 00:00:00'] = 10
timeseries.m_ice.loc['2018-01-01 00:00:00'] = 1
timeseries.m_water.loc['2018-01-01 00:00:00'] = 1
print(timeseries)

Result:

                    temperature state_of_charge m_ice m_water
2018-01-01 00:00:00         NaN             NaN   NaN     NaN
2018-01-01 05:00:00         NaN             NaN   NaN     NaN
2018-01-01 10:00:00         NaN             NaN   NaN     NaN
2018-01-01 15:00:00         NaN             NaN   NaN     NaN
2018-01-01 20:00:00         NaN             NaN   NaN     NaN
                    temperature state_of_charge m_ice m_water
2018-01-01 00:00:00          15              10     1       1
2018-01-01 05:00:00         NaN             NaN   NaN     NaN
2018-01-01 10:00:00         NaN             NaN   NaN     NaN
2018-01-01 15:00:00         NaN             NaN   NaN     NaN
2018-01-01 20:00:00         NaN             NaN   NaN     NaN

you can try to iterate over the length of the data frame and use integer index location to fill in the values into the columns. the values can be a calculated values, it does not really matter. The logic will remain the same.

import pandas as pd
timeseries = pd.DataFrame(
                columns = ["temperature", "state_of_charge", "m_ice", "m_water"],
                index =pd.date_range(start ='01-01-2018 00:00:00',
                         end ='01-02-2018 00:00:00', freq='0.25H')
                )

for i in range(len(timeseries)):
   # this an example, can be any calculated value or function return # 
    timeseries.temperature.iloc[i] = i 
    timeseries.state_of_charge.iloc[i] = i+1
    timeseries.m_ice.iloc[i] = i+2
    timeseries.m_water.iloc[i] = i+3
print(timeseries)

                    temperature state_of_charge m_ice m_water
2018-01-01 00:00:00           0               1     2       3
2018-01-01 00:15:00           1               2     3       4
2018-01-01 00:30:00           2               3     4       5
2018-01-01 00:45:00           3               4     5       6
2018-01-01 01:00:00           4               5     6       7
...                         ...             ...   ...     ...
2018-01-01 23:00:00          92              93    94      95
2018-01-01 23:15:00          93              94    95      96
2018-01-01 23:30:00          94              95    96      97
2018-01-01 23:45:00          95              96    97      98
2018-01-02 00:00:00          96              97    98      99


Related