ValueError when using pandas date_range in for loop
I have an original dataset with projects containing data about its start date, end date, total value. I calculate dayvalue (dagwaarde) by dividing total value with amount of days.
I want to fill a dataset with data 1-1-2018 to 1-1-2020 with the sum of all day values of all projects for each day.
It all works, except for this piece:
df_date_range = pd.date_range(begin,einde)
which give this error: ValueError: NaTType does not support time
This is the code I use:
#Original DF with data about projects: startdatum (start), einddatum (end), dagwaarde (day value).
#Day value is total value ('Value') / amount of days
Pipedrive['einddatum'] = pd.to_datetime(Pipedrive['einddatum'])
Pipedrive['startdatum'] = pd.to_datetime(Pipedrive['startdatum'])
Pipedrive['Days'] = Pipedrive['einddatum'].sub(Pipedrive['startdatum'], axis =0)
Pipedrive.head()
Pipedrive['Days'] = Pipedrive['Days'] / np.timedelta64(1, 'D')
Pipedrive['dagwaarde'] = Pipedrive['Value'] / Pipedrive['Days']
#Create DF to work with
Pipedrive_IN = Pipedrive[["stage_order_nr","dagwaarde",'einddatum', 'startdatum', 'Days' ]]
#make a list of all begin and end dates you want to have filled
begin = '2018-01-01' # start date
einde = '2020-01-01' # end date
#make a DF with a timedate index
datetimeindex = pd.date_range(begin,einde)
df_dates = pd.DataFrame(datetimeindex, columns=['date'])
df_dates = df_dates.set_index('date')
df_dates = df_dates.fillna(0)
for index, value in Pipedrive_IN.iterrows():
begin = value.startdatum # start date
einde = value.einddatum # end date
dagwaarde = value.dagwaarde # dagwaarde
#make DF with timedate index
df_date_range = pd.date_range(begin,einde)
df_proj = pd.DataFrame(df_date_range, columns=['date'])
df_proj['dagwaarde'] = dagwaarde
df_proj = df_proj.set_index('date')
df_proj=df_proj.dropna()
print(df_proj.head())
#add original DF to df_dates
df_dates = df_dates.join(df_proj,lsuffix='', rsuffix=index)
df_dates = df_dates.fillna(0)
print(df_dates.head(20))
#print result
df_dates['total']=df_dates.sum(axis=1)
print(df_dates.head(50))
python-3.x pandas for-loop date-range
add a comment |
I have an original dataset with projects containing data about its start date, end date, total value. I calculate dayvalue (dagwaarde) by dividing total value with amount of days.
I want to fill a dataset with data 1-1-2018 to 1-1-2020 with the sum of all day values of all projects for each day.
It all works, except for this piece:
df_date_range = pd.date_range(begin,einde)
which give this error: ValueError: NaTType does not support time
This is the code I use:
#Original DF with data about projects: startdatum (start), einddatum (end), dagwaarde (day value).
#Day value is total value ('Value') / amount of days
Pipedrive['einddatum'] = pd.to_datetime(Pipedrive['einddatum'])
Pipedrive['startdatum'] = pd.to_datetime(Pipedrive['startdatum'])
Pipedrive['Days'] = Pipedrive['einddatum'].sub(Pipedrive['startdatum'], axis =0)
Pipedrive.head()
Pipedrive['Days'] = Pipedrive['Days'] / np.timedelta64(1, 'D')
Pipedrive['dagwaarde'] = Pipedrive['Value'] / Pipedrive['Days']
#Create DF to work with
Pipedrive_IN = Pipedrive[["stage_order_nr","dagwaarde",'einddatum', 'startdatum', 'Days' ]]
#make a list of all begin and end dates you want to have filled
begin = '2018-01-01' # start date
einde = '2020-01-01' # end date
#make a DF with a timedate index
datetimeindex = pd.date_range(begin,einde)
df_dates = pd.DataFrame(datetimeindex, columns=['date'])
df_dates = df_dates.set_index('date')
df_dates = df_dates.fillna(0)
for index, value in Pipedrive_IN.iterrows():
begin = value.startdatum # start date
einde = value.einddatum # end date
dagwaarde = value.dagwaarde # dagwaarde
#make DF with timedate index
df_date_range = pd.date_range(begin,einde)
df_proj = pd.DataFrame(df_date_range, columns=['date'])
df_proj['dagwaarde'] = dagwaarde
df_proj = df_proj.set_index('date')
df_proj=df_proj.dropna()
print(df_proj.head())
#add original DF to df_dates
df_dates = df_dates.join(df_proj,lsuffix='', rsuffix=index)
df_dates = df_dates.fillna(0)
print(df_dates.head(20))
#print result
df_dates['total']=df_dates.sum(axis=1)
print(df_dates.head(50))
python-3.x pandas for-loop date-range
add a comment |
I have an original dataset with projects containing data about its start date, end date, total value. I calculate dayvalue (dagwaarde) by dividing total value with amount of days.
I want to fill a dataset with data 1-1-2018 to 1-1-2020 with the sum of all day values of all projects for each day.
It all works, except for this piece:
df_date_range = pd.date_range(begin,einde)
which give this error: ValueError: NaTType does not support time
This is the code I use:
#Original DF with data about projects: startdatum (start), einddatum (end), dagwaarde (day value).
#Day value is total value ('Value') / amount of days
Pipedrive['einddatum'] = pd.to_datetime(Pipedrive['einddatum'])
Pipedrive['startdatum'] = pd.to_datetime(Pipedrive['startdatum'])
Pipedrive['Days'] = Pipedrive['einddatum'].sub(Pipedrive['startdatum'], axis =0)
Pipedrive.head()
Pipedrive['Days'] = Pipedrive['Days'] / np.timedelta64(1, 'D')
Pipedrive['dagwaarde'] = Pipedrive['Value'] / Pipedrive['Days']
#Create DF to work with
Pipedrive_IN = Pipedrive[["stage_order_nr","dagwaarde",'einddatum', 'startdatum', 'Days' ]]
#make a list of all begin and end dates you want to have filled
begin = '2018-01-01' # start date
einde = '2020-01-01' # end date
#make a DF with a timedate index
datetimeindex = pd.date_range(begin,einde)
df_dates = pd.DataFrame(datetimeindex, columns=['date'])
df_dates = df_dates.set_index('date')
df_dates = df_dates.fillna(0)
for index, value in Pipedrive_IN.iterrows():
begin = value.startdatum # start date
einde = value.einddatum # end date
dagwaarde = value.dagwaarde # dagwaarde
#make DF with timedate index
df_date_range = pd.date_range(begin,einde)
df_proj = pd.DataFrame(df_date_range, columns=['date'])
df_proj['dagwaarde'] = dagwaarde
df_proj = df_proj.set_index('date')
df_proj=df_proj.dropna()
print(df_proj.head())
#add original DF to df_dates
df_dates = df_dates.join(df_proj,lsuffix='', rsuffix=index)
df_dates = df_dates.fillna(0)
print(df_dates.head(20))
#print result
df_dates['total']=df_dates.sum(axis=1)
print(df_dates.head(50))
python-3.x pandas for-loop date-range
I have an original dataset with projects containing data about its start date, end date, total value. I calculate dayvalue (dagwaarde) by dividing total value with amount of days.
I want to fill a dataset with data 1-1-2018 to 1-1-2020 with the sum of all day values of all projects for each day.
It all works, except for this piece:
df_date_range = pd.date_range(begin,einde)
which give this error: ValueError: NaTType does not support time
This is the code I use:
#Original DF with data about projects: startdatum (start), einddatum (end), dagwaarde (day value).
#Day value is total value ('Value') / amount of days
Pipedrive['einddatum'] = pd.to_datetime(Pipedrive['einddatum'])
Pipedrive['startdatum'] = pd.to_datetime(Pipedrive['startdatum'])
Pipedrive['Days'] = Pipedrive['einddatum'].sub(Pipedrive['startdatum'], axis =0)
Pipedrive.head()
Pipedrive['Days'] = Pipedrive['Days'] / np.timedelta64(1, 'D')
Pipedrive['dagwaarde'] = Pipedrive['Value'] / Pipedrive['Days']
#Create DF to work with
Pipedrive_IN = Pipedrive[["stage_order_nr","dagwaarde",'einddatum', 'startdatum', 'Days' ]]
#make a list of all begin and end dates you want to have filled
begin = '2018-01-01' # start date
einde = '2020-01-01' # end date
#make a DF with a timedate index
datetimeindex = pd.date_range(begin,einde)
df_dates = pd.DataFrame(datetimeindex, columns=['date'])
df_dates = df_dates.set_index('date')
df_dates = df_dates.fillna(0)
for index, value in Pipedrive_IN.iterrows():
begin = value.startdatum # start date
einde = value.einddatum # end date
dagwaarde = value.dagwaarde # dagwaarde
#make DF with timedate index
df_date_range = pd.date_range(begin,einde)
df_proj = pd.DataFrame(df_date_range, columns=['date'])
df_proj['dagwaarde'] = dagwaarde
df_proj = df_proj.set_index('date')
df_proj=df_proj.dropna()
print(df_proj.head())
#add original DF to df_dates
df_dates = df_dates.join(df_proj,lsuffix='', rsuffix=index)
df_dates = df_dates.fillna(0)
print(df_dates.head(20))
#print result
df_dates['total']=df_dates.sum(axis=1)
print(df_dates.head(50))
python-3.x pandas for-loop date-range
python-3.x pandas for-loop date-range
asked Dec 28 '18 at 7:34
MikeMike
15
15
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53955143%2fvalueerror-when-using-pandas-date-range-in-for-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53955143%2fvalueerror-when-using-pandas-date-range-in-for-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown