Add a row to a dataframe

spreadsheet = DataFrame(
date=["Jan 1, 2019"], #start of the new year
	
	
	#List of Accounts # Start of 2019
	
	


#Assets
	
chequing=[0], #replace with balance as of Jan 1, 2019

accounts_reciveable=[0], #replace with balance as of Jan 1, 2019
	
#Debts
#Debts--Credit when owed payment, Debit when recive payment

accounts_payable= [0],# be sure to add account as of Jan1, 2019


#Expenses -- Debit
	
personal=[0],

#income credit
	
wages=[0],

rent=[0],

cattle_sales=[0],
	

#Debts--Credit when owed payment, Debit when recive payment


#memo

memo=["puchase item and place"]
	);

defaultRow = (DataFrame(
date=[""],
chequing=[0],
accounts_recivable=[0],
accounts_payable=[0],
personal=[0],
wages=[0],
rent=[0],
catle_sales=[0],
memo=[""]))

macro transaction(
		Journal_Entry,
		newdate,
		newdebit,
		debit_amount,
		newcredit, 
		credit_amount,
		new_memo)
	
	ex=quote
 	   if $credit_amount != $debit_amount
			"Error--Debit and Credit must match"
       else
			
				push!(spreadsheet,(defaultRow...,
					$newcredit=$credit_amount,		
					$newdebit= $debit_amount,
					Date=$newdate,
					memo=$new_memo
						)) 


			#figure out how to delete this row, and get around repeat variables
			# I might have to save each transaction to the spreadsheet, at this stage
		end
	end
	esc(ex)
	
end

@transaction(JE4,"Jan 1, 2019", wages, 1000, chequing, 1000,"this is a test")

Why can’t I get this to work, should the default row be a data frame? Is there a difference in the number of variables? I counted them 20 times. Each has 9 variables, maybe there is a spelling error?

According to @macroexpand, that expands to

julia> push!(spreadsheet, (defaultRow..., chequing = 1000, wages = 1000, Date = "Jan 1, 2019", memo = "this is a test"))
ERROR: AbstractDataFrame is not iterable. Use eachrow(df) to get a row iterator or eachcol(df) to get a column iterator

The error is happening in the defaultRow..., segment.

This will be easier to see if you don’t use a macro. A function seems like a better fit.

2 Likes

This whole thing was a mess. I got it all to work,

Might look at

1 Like

Is metaprograming different than macros?

I’d say macros are a prominent kind of metaprogramming (there are other kinds). Generated functions are another (though often created using macros).

I can see where that is useful. I think I can do almost everything with a macro, and I want to get that figured out first. The problem I’m having now, is entering the name of an array or a DataFrame column as a string.

To be clear, my point is that macros are almost always unnecessary and create confusion. I think this is an example of that: unless I’m misunderstanding your goal, there is no need to use a macro here, and you shouldn’t use one. A function (defined with function) would be fine.

3 Likes

Here (in this thread, out of 3) we get the defaultRow shown, which I was missing in the other thread and which I asked for. With this information, I can try to help perhaps a step further.

I understand it like, defaultRow holds the default information, which should be pushed into the DataFrame, if only parts of the new row is given. With that, defaultRow should be possible to be pushed, even if no information is given:

julia> push!(spreadsheet,defaultRow)
ERROR: ArgumentError: `push!` does not allow passing collections of type DataFrame to be pushed into a DataFrame. Only `Tuple`, `AbstractArray`, `AbstractDict`, `DataFrameRow` and `NamedTuple` are allowed.

From this the answer is clear: not a DataFrame can be pushed, only a DataFrameRow. So:

julia> push!(spreadsheet,defaultRow[1,:])
┌ Error: Error adding value to column :accounts_reciveable.
ERROR: ArgumentError: column name :accounts_reciveable not found in the data frame; existing most similar names are: :accounts_recivable

Again clear, a typo, fixing that:

defaultRow = (DataFrame(
date=[""],
chequing=[0],
accounts_reciveable=[0],
accounts_payable=[0],
personal=[0],
wages=[0],
rent=[0],
catle_sales=[0],
memo=[""]))

julia> push!(spreadsheet,defaultRow[1,:])
┌ Error: Error adding value to column :cattle_sales.
ERROR: ArgumentError: column name :cattle_sales not found in the data frame; existing most similar names are: :catle_sales

Another one, fixing that too:

defaultRow = (DataFrame(
date=[""],
chequing=[0],
accounts_reciveable=[0],
accounts_payable=[0],
personal=[0],
wages=[0],
rent=[0],
cattle_sales=[0],
memo=[""]))

julia> push!(spreadsheet,defaultRow[1,:])
2×9 DataFrame
...

Does this help?
Two points:

  • To stress the point: macros aren’t the ultimate tool to achieve code reuse, it’s functions which do this.
  • It’s currently not clear, if your problem is solved or not.
1 Like